IOS Swift 4中的TableView CheckMark和Uncheck With滚动仍选中的单元格值 [英] TableView CheckMark and Uncheck With Scroll Up Still Checked Cell Value In Ios Swift 4

查看:107
本文介绍了IOS Swift 4中的TableView CheckMark和Uncheck With滚动仍选中的单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向上滚动后删除的TableView CheckMark单元格值将修复 向上滚动然后向下滚动以显示您的Checkmark单元将被删除,因为该单元是dequeueReusableCell,因此您遇到的TableView多次遇到了Checkmark问题,因此,此问题解决方法只是将您的代码放入并解决了问题.

TableView CheckMark Cell Value Removed After Scrolling Up It will Fix TableView in You have face a problem many times to Checkmark after scroll Up then Scroll Down To show a Your Checkmark cell is will Removed Because cell is dequeueReusableCell So This Problem Fix , you Have just put Your code and Solved Your Problem.

有更多帮助,请发送按摩. 太感谢了. :)

Any More Help So Send Massage. Thank you So much. :)

class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate{

var temp = [Int]()
var numarr = [Int]()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return numarr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "id")
    cell = UITableViewCell.init(style: .default, reuseIdentifier: "id")
    cell?.textLabel?.text = String(numarr[indexPath.row])
    if temp.contains(numarr[indexPath.row] as Int)
    {
        cell?.accessoryType = .checkmark
    }
    else
    {
        cell?.accessoryType = .none
    }
    return cell!
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath)
    if temp.contains(numarr[indexPath.row] as Int)
    {
        cell?.accessoryType = .none
        temp.remove(at: temp.index(of: numarr[indexPath.row])!)
    }
    else
    {
        cell?.accessoryType = .checkmark
        temp.append(self.numarr[indexPath.row] as Int)
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    for i in 1...100
    {
        numarr.append(i)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

推荐答案

我认为,如果有人要运行您的代码,它将不会显示任何错误.但是如果有真实数据,它可能会. 原因存储对勾标记的方式.您应该将数组的实际值indexPath存储到temp数组中时,将行的数据存储到temp数组中,以便只有该行具有复选标记.在您的情况下,如果一行的标签内有1并单击它,则该单元格将突出显示.现在,如果您开始滚动并且另一个单元格包含1,则该行也将突出显示.

I think if someone were to run your code it would not show any error. But with real data it probably will. The reason is the way you store your checkmarks. You store the data of a row into the temp array when you should be storing the actualy indexPath of the array so that only that row gets the checkmark. In your case, if a row has 1 inside it's label and you click on it, that cell will be highlighted. Now if you start scrolling and another cell contains 1 then that row will also be highlighted.

对于单节的情况,我已经修改了您的示例.如果有多个部分,则需要存储indexPath而不是indexPath.row.

I have modified your example for the case of a single section. If there is more than one section, you need to store the indexPath instead of indexPath.row.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "id")
    cell = UITableViewCell.init(style: .default, reuseIdentifier: "id")
    cell?.textLabel?.text = String(numarr[indexPath.row])
    if temp.contains(indexPath.row) {
        cell?.accessoryType = .checkmark
    } else {
        cell?.accessoryType = .none
    }
    return cell!
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath)
    if temp.contains(indexPath.row) {
        cell?.accessoryType = .none
        temp.remove(at: indexPath.row)
    } else {
        cell?.accessoryType = .checkmark
        temp.append(indexPath.row)
    }
}

这篇关于IOS Swift 4中的TableView CheckMark和Uncheck With滚动仍选中的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆