【发布时间】:2014-09-21 21:41:42
【问题描述】:
我在我的应用程序的其中一个表格视图中的选择时遇到了问题。基本上,问题是我只能通过点击作为搜索结果的单元格来切换到另一个视图控制器。出于某种原因,我无法点击标准表格视图中的单元格。
当我正常点击一个单元格时,什么也没有发生。但是,如果我使用搜索栏搜索单元格,我可以点击单元格,它们将按照预期转移到另一个视图控制器。我不知道为什么。
这是我的代码:
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if tableView == self.searchDisplayController?.searchResultsTableView {
return searchResults.count
} else {
if names.count == 0 {
if enterButtonTapped == false {
backgroundLabel.text = "Before you add any transactions, you must first set a budget. You can do this by tapping the 'Budget' tab."
} else {
backgroundLabel.text = "You haven't added any transactions yet. Tap the add button to add a new transaction."
}
backgroundLabel.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
backgroundLabel.numberOfLines = 0
backgroundLabel.textAlignment = NSTextAlignment.Center
backgroundLabel.sizeToFit()
backgroundLabel.hidden = false
backgroundLabel.textColor = UIColor.lightGrayColor()
clearButton.enabled = false
self.tableView.backgroundView = backgroundLabel
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
self.tableView.scrollEnabled = false
return 0
} else {
backgroundLabel.hidden = true
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
self.tableView.scrollEnabled = true
clearButton.enabled = true
return names.count
}
}
}
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
}
//This works, unless there are two payments with the same name. To revert to previous version, remove tableView if statement and just keep code below the else.
} 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
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
if tableView == self.searchDisplayController?.searchResultsTableView {
return false
} else {
return true
}
}
视图控制器通过 push segue 连接到情节提要中的单元格。
最初我想知道是不是这条线(但不是):
cell.selectionStyle = UITableViewCellSelectionStyle.None
有什么想法吗?
【问题讨论】:
-
您很可能有一个或多个元素覆盖了大部分拦截触摸的表格视图单元格。
-
单元格上确实有元素,但它们并没有覆盖整个东西,所以即使点击空白空间也不起作用。另外,如果是这样的话,搜索结果也会有问题吗?
-
您似乎有两个表视图 - 搜索表视图和 self.tableview。我假设这是因为
cellForRowAtIndexPath顶部的if- 但你总是从self.tableview中取出单元格。如果你有多个表视图,你应该将tableview传递给dequeueReusableCell...。 -
啊,我明白了。抱歉,我不太明白您将 tableview 传递给 dequeueReusableCell 的意思?
-
有一个tableview参数传入
cellForRowAtIndexPath——这是你当前正在操作的tableview,所以你应该调用varcell:CustomTransactionTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTransactionTableViewCell
标签: ios uitableview swift