Search code examples
iosswiftuitableviewswift4xcode10

I want to set tick in tableview cell for row at indexpath


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

Solution

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

    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
    }
    

    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
    
    }
    

    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.