0
我一直在我的应用程序中的某个表格视图中选择问题。基本上,问题是我只能通过点击作为搜索结果的单元格才能继续访问另一个视图控制器。出于某种原因,我无法在标准表格视图中点按单元格。在表格视图中点击单元格不会执行任何操作
当我正常点击一个单元时,没有任何反应。但是,如果我使用搜索栏搜索单元格,则可以点击单元格,并且它们将按照预期继续到另一个视图控制器。我无法弄清楚为什么。
这是我的代码:
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
}
}
视图控制器通过推SEGUE连接到故事板的单元格。
起初我怀疑这是此行(但它不是):
cell.selectionStyle = UITableViewCellSelectionStyle.None
任何想法?
您最有可能拥有一个或多个元素来覆盖截取触摸的大多数表视图单元格。 – rebello95 2014-09-21 21:44:03
确实存在单元格上的元素,但它们并未涵盖整个事物,因此即使点击空白区域也不起作用。另外,当然如果是这样的话,这对搜索结果也是一个问题? – user3746428 2014-09-21 21:49:48
看起来你有两个表视图 - 搜索表视图和self.tableview。我假设这是因为'cellForRowAtIndexPath'顶部的'if' - 但是你总是从'self.tableview'取出单元格。如果你有多个tableviews,你只需要传递'tableview'到'dequeueReusableCell ...'。 – Paulw11 2014-09-21 21:51:29