【问题标题】:UITableViewCell Checkmark Being Added to multiple rows when one is tapped当一个被点击时,UITableViewCell 复选标记被添加到多行
【发布时间】:2015-09-21 13:55:32
【问题描述】:

我有一个表格视图,其中并非所有单元格都一次可见。我正在努力做到这一点,以便当点击一行时,它会在单元格中添加一个复选标记附件。我的问题是它也将它添加到其他行。在我的表格视图中,有 4 行完全显示,而第五行几乎没有显示。如果我选中第一个框,它会在每五个框添加一个复选标记(例如 indexPath.row = 0,5,10,15...),尽管 indexPath.row 不同。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

     let cell: DropDownMenuCell = tableView.dequeueReusableCellWithIdentifier("DropDownMenuCell", forIndexPath: indexPath) as! DropDownMenuCell
     cell.dropDownCellLabel?.text = DropDownItems[indexPath.row].Name
     return cell

}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let selectedCell: DropDownMenuCell = tableView.cellForRowAtIndexPath(indexPath) as! DropDownMenuCell
    print(indexPath.row)
    if selectedCell.accessoryType == .None {
        selectedCell.accessoryType = .Checkmark
    } else {
        selectedCell.accessoryType = .None
    }

}

编辑:为重复道歉,我对这个问题的最初搜索没有显示其他问题。我已经在这里迅速得到了一个有效的答案,或者我会尝试通过目标 c 帖子来解决我的问题。

【问题讨论】:

  • Tableview 单元格被重复使用。您需要维护已被点击的单元格的索引数组,并根据您的数组在tableView:cellForRowAtIndexPath: 中设置复选标记。如果您搜索 StackOverflow,您会发现有关单元重用的其他问题。

标签: ios swift uitableview


【解决方案1】:

在您的数据源中维护要选择的单元格。

然后在 cellForRowAtIndexPath:

if (DropDownItems[indexPath.row].isSelected) {
    cell.accessoryType = .Checkmark
} else {
    cell.accessoryType = .None
}

在你的 didSelectRowAtIndexPath 方法中:

if(DropDownItems[indexPath.row].isSelected) {
    DropDownItems[indexPath.row].isSelected = false
} else {
    DropDownItems[indexPath.row].isSelected = true
}

self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)

【讨论】:

    【解决方案2】:

    在 Swift 3 中,这应该会有所帮助:

    import UIKit
    
    class ViewController: UITableViewController {
    
    let foods = ["apple", "orange", "banana", "spinach", "grape"]
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    
    {
        return foods.count
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
        UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = foods[indexPath.row]
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark
        {
            tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
        }
        else
        {
            tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多