我一直在尝试在我的应用程序中实现一项功能,以便当用户在我的表格视图中点击一个单元格时,该单元格会向下扩展以显示注释。我在Objective- C中找到了很多这样的例子,但是我还没有找到Swift的例子。
我试图将其翻译为Swift:
var selectedRowIndex = NSIndexPath() override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { selectedRowIndex = indexPath tableView.beginUpdates() tableView.endUpdates() } override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if selectedRowIndex == selectedRowIndex.row && indexPath.row == selectedRowIndex.row { return 100 } return 70 }
但是,这似乎使应用程序崩溃了。
有任何想法吗?
编辑:
这是我的cellForRowAtIndexPath代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell:CustomTransactionTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTransactionTableViewCell cell.selectionStyle = UITableViewCellSelectionStyle.None if tableView == self.searchDisplayController?.searchResultsTableView { cell.paymentNameLabel.text = (searchResults.objectAtIndex(indexPath.row)) as? String //println(searchResults.objectAtIndex(indexPath.row)) var indexValue = names.indexOfObject(searchResults.objectAtIndex(indexPath.row)) cell.costLabel.text = (values.objectAtIndex(indexValue)) as? String cell.dateLabel.text = (dates.objectAtIndex(indexValue)) as? String if images.objectAtIndex(indexValue) as NSObject == 0 { cell.paymentArrowImage.hidden = false cell.creditArrowImage.hidden = true } else if images.objectAtIndex(indexValue) as NSObject == 1 { cell.creditArrowImage.hidden = false cell.paymentArrowImage.hidden = true } } else { cell.paymentNameLabel.text = (names.objectAtIndex(indexPath.row)) as? String cell.costLabel.text = (values.objectAtIndex(indexPath.row)) as? String cell.dateLabel.text = (dates.objectAtIndex(indexPath.row)) as? String if images.objectAtIndex(indexPath.row) as NSObject == 0 { cell.paymentArrowImage.hidden = false cell.creditArrowImage.hidden = true } else if images.objectAtIndex(indexPath.row) as NSObject == 1 { cell.creditArrowImage.hidden = false cell.paymentArrowImage.hidden = true } } return cell }
这是插座设置:
if语句中的第一个比较永远不会为真,因为您正在将indexPath与整数进行比较。您还应该使用表中不能包含的行值来初始化selectedRowIndex变量,例如-1,因此在表首次加载时不会展开任何内容。
var selectedRowIndex: NSIndexPath = NSIndexPath(forRow: -1, inSection: 0) override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if indexPath.row == selectedRowIndex.row { return 100 } return 70 }
斯威夫特4.2 var selectedRowIndex: NSIndexPath = NSIndexPath(row: -1, section: 0)
var selectedRowIndex: NSIndexPath = NSIndexPath(row: -1, section: 0)