Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

ios - 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)
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...