【问题标题】:I want to set tick in tableview cell for row at indexpath我想在 tableview 单元格中为 indexpath 的行设置刻度
【发布时间】:2019-04-05 11:00:19
【问题描述】:

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

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)
}

【问题讨论】:

  • 我会使用Set<IndexPath>,然后根据需要插入/删除索引路径。然后你在cellForRow(at:)中使用contains
  • 您的意思是仅使用 indexpath 将所选项目存储在数组中并进入 cellForRow(at:) 将该数组项目与 indexpath.row @Dávid Pásztor
  • 不要存储在数组中,存储在Set
  • 你有什么例子可以快速理解吗? @Paulw11
  • @Paulw11 为什么 Set 优于 Array?

标签: ios swift uitableview swift4 xcode10


【解决方案1】:

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

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

struct Model {
    var isSelected = false
    var someOtherMember : String

    // other declarations
}

在cellForRow中根据isSelected的成员打勾

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

}

在didSelectRowAt 和didDeselectRowAt 中更新模型并重新加载行

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)
}

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

【讨论】:

    【解决方案2】:
    I have used button in table...
    // declare var
     var checkarrMenu = Set<IndexPath>()
    
    // In tableView
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return array.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = mManageTableView.dequeueReusableCell(withIdentifier: "TableViewCellID")as! TableViewCell
            cell.mRadioBtnAct.tag = indexPath.row
            cell.mRadioBtnAct.addTarget(self, action: #selector(mRadioCheck(sender:)), for: .touchUpInside)
            if checkarrMenu.contains(indexPath){
                cell.mradioimage.image = UIImage(named: "Radiochecked")
            }else{
                cell.mradioimage.image = UIImage(named: "Radio checkedlightRadio3")
            }
            return cell
        }
    // on radio btn Action
     @objc func mRadioCheck(sender:UIButton){
            let touchPoint: CGPoint = sender.convert(CGPoint.zero, to: mManageTableView)
            // maintable --> replace your tableview name
            let clickedButtonIndexPath = mManageTableView.indexPathForRow(at: touchPoint)
            if checkarrMenu.contains(clickedButtonIndexPath){
                checkarrMenu.remove(clickedButtonIndexPath)
    
            }else{
                checkarrMenu.insert(clickedButtonIndexPath)
    
                }
            tableView.reloadData()
           }
    // hope its works for you!
    

    【讨论】:

    • 不要在 Swift 中使用 NSMutableArray,除非 Objective C 互操作性需要
    • okk 那么我将使用一组 indexPath Set() ,回答已更新
    • @Paulw11 所以我们可以使用什么来代替这个 NSMutableArray,看起来他正在存储按钮位置,这很像 indexpath 行,我尝试了他的代码,我得到的结果类似于该数组的输出 - > (" {length = 2, path = 0 - 0}"), (" {length = 2, path = 1 - 0}")
    猜你喜欢
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 2016-10-19
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 2015-08-01
    相关资源
    最近更新 更多