我想在 tableview 单元格中为 indexpath 处的行设置刻度 [英] I want to set tick in tableview cell for row at indexpath

查看:31
本文介绍了我想在 tableview 单元格中为 indexpath 处的行设置刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tableview 中添加多项选择和 tableview 中的部分,那么当用户单击将作为选定项存储在数组中的特定部分内的单元格时,我如何存储该部分索引以及该单元格的索引路径以供显示在 cellForRowAtindexpath 中反映更好的解决方案是值得赞赏的

i am adding multiple selection in tableview with sections in tableview, so how can i store that section index as well as indexpath of that cell for display when user click on cell inside that particular sections that will store in array as selected item and reflect in cellForRowAtindexpath the better solution is appreciated

func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return itemsInSections[section].count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.sections.count;
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor(red: 8/255, green: 38/255, blue: 76/255, alpha: 1.0)
    header.backgroundColor = UIColor.clear

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tablheaders.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
    let section = indexPath.section
    if checkdarray.count > 0
    {
        if (self.checkdarray[section][indexPath.row] as Int == 1)
        {
            cell.accessoryType = .checkmark;
        }
        else
        {
            cell.accessoryType = .none;
        }
    }

    cell.textLabel?.text = itemsInSections[indexPath.section][indexPath.row] as! String

    return cell

}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     let section = indexPath.section as Int
    let indepaths = indexPath.row as Int
    tableView.cellForRow(at: indexPath as IndexPath)?.accessoryType = .checkmark
    checkdarray.insert([1], at: [section][indepaths])
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    tableView.cellForRow(at: indexPath as IndexPath)?.accessoryType = .none
   // checkdarray.remove(at: indexPath.row)

    //checkdarray.insert(0, at: indexPath.row)
}

推荐答案

最有效、最可靠的解决方案是在数据模型中保持 isSelected 状态.

The most efficient and reliable solution is to keep the isSelected state in the data model.

使用结构作为数据模型并添加成员 isSelected 以及您需要的其他信息.

Use a struct as data model and add a member isSelected along with the other information you need.

struct Model {
    var isSelected = false
    var someOtherMember : String

    // other declarations
}

cellForRow中根据isSelected成员设置复选标记

In cellForRow set the checkmark according to the isSelected member

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) // use always the passed tableView instance

    let model = itemsInSections[indexPath.section][indexPath.row]
    cell.accessoryType = model.isSelected ? .checkmark : .none
    cell.textLabel?.text = model.someOtherMember

    return cell

}

didSelectRowAtdidDeselectRowAt 中更新模型并重新加载行

In didSelectRowAt and didDeselectRowAt update the model and reload the row

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    itemsInSections[indexPath.section][indexPath.row].isSelected = true
    tableView.reloadRows(at: [indexPath], with: .none)
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    itemsInSections[indexPath.section][indexPath.row].isSelected = false
    tableView.reloadRows(at: [indexPath], with: .none)
}

从不使用额外的数组来保存索引路径.不要那样做.

Never use an extra array to save index paths. Don't do that.

这篇关于我想在 tableview 单元格中为 indexpath 处的行设置刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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