如何对字符串进行索引?- Swift 3
如何对字符串进行索引?- Swift 3
这个问题已经有答案了:
我是新手,需要您的帮助。请问如何更改此代码,以产生一个字符串数组而不是整数数组。感谢您的时间和任何帮助。
import UIKit class ViewController: UIViewController { @IBOutlet weak var objTable: UITableView! var numberArray = NSMutableArray() var selectedArray=NSMutableArray() override func viewDidLoad() { super.viewDidLoad() for index in 1...200 { numberArray.add(index) } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return numberArray.count; } func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell { let contact = numberArray.object(at: indexPath.row) let cell:MyCustomClass = objTable.dequeueReusableCell(withIdentifier: "reuseCell") as! MyCustomClass cell.textLabel?.text = String("Number \(contact)") cell.tickButton.addTarget(self, action:#selector(ViewController.tickClicked(_:)), for: .touchUpInside) cell.tickButton.tag=indexPath.row if selectedArray .contains(numberArray.object(at: indexPath.row)) { cell.tickButton.setBackgroundImage(UIImage(named:"Select.png"), for: UIControlState()) } else { cell.tickButton.setBackgroundImage(UIImage(named:"Diselect.png"), for: UIControlState()) } return cell } func tickClicked(_ sender: UIButton!) { let value = sender.tag; if selectedArray.contains(numberArray.object(at: value)) { selectedArray.remove(numberArray.object(at: value)) } else { selectedArray.add(numberArray.object(at: value)) } print("Selected Array \(selectedArray)") objTable.reloadData() } func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: IndexPath) ->CGFloat { return 80.0 } }
admin 更改状态以发布 2023年5月22日
试一下:
let stringArray: [String] = (1...200).map {"Number " + String(format: "%d", $0)}
然后删除这一行:
let contact = numberArray.object(at: indexPath.row)
并将这一行改变:
cell.textLabel?.text = String("Number \(contact)")
为:
cell.textLabel?.text = stringArray[indexPath.row]
更新:
import UIKit class ViewController: UIViewController { @IBOutlet weak var objTable: UITableView! // var numberArray = NSMutableArray() let stringArray: [String] = (1...200).map {"Number " + String(format: "%d", $0)} var selectedArray=NSMutableArray() override func viewDidLoad() { super.viewDidLoad() // for index in 1...200 { // numberArray.add(index) // } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // return numberArray.count; return stringArray.count; } func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell { // let contact = numberArray.object(at: indexPath.row) let cell:MyCustomClass = objTable.dequeueReusableCell(withIdentifier: "reuseCell") as! MyCustomClass // cell.textLabel?.text = String("Number \(contact)") cell.textLabel?.text = stringArray[indexPath.row] cell.tickButton.addTarget(self, action:#selector(ViewController.tickClicked(_:)), for: .touchUpInside) cell.tickButton.tag = indexPath.row if selectedArray .contains(stringArray[indexPath.row]) { cell.tickButton.setBackgroundImage(UIImage(named:"Select.png"), for: UIControlState()) } else { cell.tickButton.setBackgroundImage(UIImage(named:"Diselect.png"), for: UIControlState()) } return cell } func tickClicked(_ sender: UIButton!) { let value = sender.tag; if selectedArray.contains(stringArray[value]) { selectedArray.remove(stringArray[value]) } else { selectedArray.add(stringArray[value]) } print("Selected Array \(selectedArray)") objTable.reloadData() } func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: IndexPath) ->CGFloat { return 80.0 } }