我正在使用 Swift。我有一个 tableView
,我希望用户选择多个选项。当用户点击一行时,我希望它突出显示,然后允许他们选择更多行。
我的代码如下:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
var allCreditCard : AllCreditCard
allCreditCard = self.creditCard[indexPath.row]
cell.textLabel?.text = allCreditCard.bankName
// if(cell.selected){
// println("cell selected at \(indexPath.row)");
// let imageName = "tick.png";
// let image: UIImageView = UIImageView(image: UIImage(named: imageName));
// cell.accessoryView = image;
// }else{
// println("cell was NOT selected. Index path----- \(indexPath.row)");
// let image: UIImageView = UIImageView();
// cell.accessoryView = image;
// }
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
我该如何解决这个问题。我不确定如何添加 rowIndex
es,然后用更新的行填充表。有人可以帮我吗。
最佳答案
获取一个数组并将所有选定的 indexPath.row
添加到 didSelectRowAtIndexPath
上
并在 cellForRowAtIndexPath
中检查该数组中的所有对象并将单元格的 AccessoryType 设置为 UITableViewCellAccessoryCheckmark
,
if (selectedIndex==indexPath.row) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
你可以快速完成,逻辑是一样的
关于ios - 多个 TableView 行选择 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27397387/