繁体   English   中英

在Swift中的cellForRowAtIndexPath中的UITableView中选择检测单元

[英]Detecting cell is selected in UITableView in cellForRowAtIndexPath in Swift

我试图检测是否在cellForRowAtIndexPath选择了UITableViewCell 到目前为止,这是我对目标C所做的工作:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:someIndexPath];
if(cell.isSelected) {
    NSlog(@"This cell is selected")
}

我无法访问Swift中UITableviewCell isSelected属性。 我该如何实现?

if cell.selected {是正确的方法。

if let cell = tableView.cellForRowAtIndexPath(someIndexPath) {
  if cell.selected {
    print("This cell is selected")
  }
}

更新:Swift 3

if let cell = tableView.cellForRow(at: someIndexPath) {
  if cell.isSelected {
    print("This cell is selected")
  }
}

您需要实现委托方法didSelectRowAtIndexPath

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    print("cell selected at row \(indexPath.row)")
}

您可以这样操作,获取indexPath

var someIndexPath: NSIndexPath = tableView.indexPathForSelectedRow()  

使用单元格的isSelected属性实际选择时取消选择。

var cell: UITableViewCell = tableView.cellForRowAtIndexPath(someIndexPath)
if cell.isSelected {
   tableView.deselectRowAtIndexPath(someIndexPath, animated: true)
}

暂无
暂无

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

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