繁体   English   中英

如何从Indexpath获取自定义单元格对象?

[英]How to get custom cell object from Indexpath?

我尝试了以下代码来获取自定义单元格。此代码用于UITableView单元格,一次仅检查一行。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // if they are selecting the same row again, there is nothing to do, just keep it checked
    if ((seleectedIndex) != nil) {

        if indexPath == seleectedIndex {
            return
        }
        let oldCell = tableView.cellForRow(at: seleectedIndex!) as! MisstageTableViewCell
        if oldCell.accessoryType == UITableViewCellAccessoryType.checkmark {
            oldCell.accessoryType = UITableViewCellAccessoryType.none
            oldCell.buttonSelection.setImage(UIImage(named:"off")?.withRenderingMode(.alwaysOriginal), for:.normal)
        }

    }
    // toggle old one off and the new one on
    let newCell = tableView.cellForRow(at: indexPath) as! MisstageTableViewCell
    if newCell.accessoryType == UITableViewCellAccessoryType.none {
        newCell.accessoryType = UITableViewCellAccessoryType.checkmark
        newCell.buttonSelection.setImage(UIImage(named:"on")?.withRenderingMode(.alwaysOriginal), for:.normal)
    }
    seleectedIndex = indexPath  // save the selected index path
}

我的代码工作正常,但是当我滚动表格视图(例如上下)时,我单击了任何单元格,然后在特定行出现了崩溃

let oldCell = tableView.cellForRow(at: seleectedIndex!) as! MisstageTableViewCell

我不知道崩溃的原因是什么。请您帮我解决问题吗?

你为什么要更新里面的单元格

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

你应该做

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

首先将变量声明为:

var selectedIndexPath: IndexPath!

didSelectRowAt应该修改如下:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if selectedIndexPath != nil {
        let tempIndexPath = selectedIndexPath
        selectedIndexPath = indexPath
        tableView.reloadRows(at: [indexPath, tempIndexPath], with: .fade)
    }
    else {
        selectedIndexPath = indexPath
        tableView.reloadRows(at: [selectedIndexPath], with: .fade)

    }
}

我不知道您在cellForRowAt中做什么。 但应进行如下修改:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.accessoryType = UITableViewCellAccessoryType.none
    cell.buttonSelection.setImage(UIImage(named:"off")?.withRenderingMode(.alwaysOriginal), for:.normal)
    if selectedIndexPath != nil && indexPath == selectedIndexPath {
        cell.accessoryType = UITableViewCellAccessoryType.checkmark
        cell.buttonSelection.setImage(UIImage(named:"on")?.withRenderingMode(.alwaysOriginal), for:.normal)
    }

    return cell
}

您不应该在didSelectRow内更改单元格数据,理想的解决方案是根据选定的indexPath和单元格的check/unckeck附件类型重新加载行。

class TableViewController: UITableViewController {
    var selectedIndex: IndexPath?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MisstageTableViewCell
        if let selectedIndex = selectedIndex, indexPath.row == selectedIndex.row {
           cell.accessoryType = .checkmark
           let image = UIImage(named: "on")
           cell.buttonSelection.setImage(image!.withRenderingMode(.alwaysOriginal), for: .normal)
        } else {
          cell.accessoryType = .none
          let image = UIImage(named: "off")

        }
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //selected the same indexPath then return
      if let selectedIndex = selectedIndex, selectedIndex == indexPath {
        return
      }

      if let selectedIndex = selectedIndex, selectedIndex != indexPath {
         let oldSelectedIndex = selectedIndex
        selectedIndex = indexPath
        tableView.reloadRows(at: [oldSelectedIndex, indexPath], with: .fade)
      } else {
        selectedIndex = indexPath
        tableView.reloadRows(at: [indexPath], with: .fade)
      }
    }
}

您强行解开一个可选的。 如果您选择一个单元格,然后滚动并且所选的单元格不再出现在屏幕上,则系统可以将其从内存中删除,因此它将变为零。

if let oldCell = tableView.cellForRow(at: seleectedIndex!) as? MisstageTableViewCell {
    // do stuff here
}

返回的结果是可选的,因此可以为nil

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM