我想在indexview

时间:2019-04-05 11:00:20

标签: ios swift uitableview

我要在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)
}

2 个答案:

答案 0 :(得分:0)

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!

答案 1 :(得分:0)

最有效,最可靠的解决方案是将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

}

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

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

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