当我想在我的手机上运行它时,在我从我的 detailViewController 来回切换之后,没有为单元格分配复选标记。他们目前没有分配复选标记,所以我希望在单元格被选中后为其分配一个复选标记。如果您知道这里发生了什么,请告诉我!谢谢
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
if cell.accessoryType == UITableViewCellAccessoryType.None
{
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
}
最佳答案
当前您正在出列一个新单元格,不一定是被选中的单元格。但是,您应该坚持是否使用模型对象检查单元格,而不是坚持 View 本身的状态。一旦单元格出队/重新使用,它将不再被选中。
例如,您有一个模型对象数组:
class YourObject {
var isSelected: Bool = false
}
var modelObjects: [YourObject] = []
在每个对象中都有一个属性表示该对象是否被选中,这将在 didSelectRowAtIndexPath
中设置。
var object = self.modelObjects[indexPath.row]
object.isSelected = true
tableView.reloadRowsAtIndexPaths([indexPath], rowAnimation: .Automatic)
在 cellForRowAtIndexPath
中,您可以检查是否选择了模型对象上的属性,如果是,则将单元格的 accessoryType 设置为.Checkmark。
var object = self.modelObjects[indexPath.row]
if object.isSelected {
cell.accessoryType .Checkmark
}
关于ios - 为什么这段代码不允许我在我的 tableViewCell 中放置一个复选标记配件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37007754/