点击时可以打开和关闭UITableViewCell复选标记

点击时可以打开和关闭UITableViewCell复选标记,第1张

点击时可以打开和关闭UITableViewCell复选标记

迅捷 > 3.0

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {    if let cell = tableView.cellForRow(at: indexPath) {        cell.accessoryType = .none    }}func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {    if let cell = tableView.cellForRow(at: indexPath) {        cell.accessoryType = .checkmark    }}

我通过使用两个Swift函数解决了:didSelectRowAtIndexPath和didDeselectRowAtIndexPath。

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {    if let cell = tableView.cellForRowAtIndexPath(indexPath) {        cell.accessoryType = .None    }}override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {    if let cell = tableView.cellForRowAtIndexPath(indexPath) {        cell.accessoryType = .Checkmark    }}

为了使其正常工作,请在您的cellForRowAtIndexPath函数中添加一行代码,以在屏幕上绘制表格视图时选择一行,否则,第一次选择另一行时,didDeselectRowAtIndexPath将不会被调用。像这样:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {    let cell = tableView.dequeueReusableCellWithIdentifier("cellData", forIndexPath: indexPath)     if (some condition to initially checkmark a row)        cell.accessoryType = .Checkmark        tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.Bottom)    } else {        cell.accessoryType = .None    }    return cell}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5639383.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存