我刚刚为我的单元格添加了一个详细视图,但我无法选择要推送到详细视图的单元格。
出于某种原因,当我正常点击其中一个细胞时,没有任何反应。但是,当我在执行搜索后点击其中一个单元格时,单元格会推送到详细视图。我查看了所有代码,但我无法弄清问题是什么。
最初我以为是这一行:
cell.selectionStyle = UITableViewCellSelectionStyle.None
但我对此进行了评论,并没有起到什么作用。
以下是相关代码(抱歉长度!):
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
}
}
override func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
if let tv=tableView {
if tableView == self.searchDisplayController.searchResultsTableView {
UITableViewCellEditingStyle.None
} else {
var valueToRemove: AnyObject! = unformatted.objectAtIndex(indexPath!.row)
//println(valueToRemove)
if images.objectAtIndex(indexPath.row) as NSObject == 0 {
totalSpendingsCounter = totalSpendingsCounter - Double(valueToRemove as NSNumber)
NSUserDefaults.standardUserDefaults().setDouble(totalSpendingsCounter, forKey: "spendingsCounter")
} else if images.objectAtIndex(indexPath.row) as NSObject == 1 {
totalCreditCounter = totalCreditCounter - Double(valueToRemove as NSNumber)
NSUserDefaults.standardUserDefaults().setDouble(totalCreditCounter, forKey: "creditCounter")
}
currencyDouble = NSUserDefaults.standardUserDefaults().doubleForKey("currencyCounter")
currentBudgetCalculation = currencyDouble + totalCreditCounter - totalSpendingsCounter
newTransactionEntered = true
var formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.locale = NSLocale.currentLocale() // This is the default
var formattedNumberCurrent = formatter.stringFromNumber(currentBudgetCalculation)
var defaults = NSUserDefaults(suiteName: "group.AffordIt")
defaults.setObject(formattedNumberCurrent, forKey: "currentBudgetWidget")
defaults.setObject(newTransactionEntered, forKey: "new")
values.removeObjectAtIndex(indexPath!.row)
images.removeObjectAtIndex(indexPath!.row)
names.removeObjectAtIndex(indexPath!.row)
dates.removeObjectAtIndex(indexPath!.row)
unformatted.removeObjectAtIndex(indexPath!.row)
NSUserDefaults.standardUserDefaults().setObject(names, forKey: "names")
NSUserDefaults.standardUserDefaults().setObject(values, forKey: "values")
NSUserDefaults.standardUserDefaults().setObject(dates, forKey: "dates")
NSUserDefaults.standardUserDefaults().setObject(unformatted, forKey: "unformatted")
NSUserDefaults.standardUserDefaults().setObject(images, forKey: "images")
tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
}
}
override func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) {
self.presentViewController(DetailViewController(), animated: true, completion: nil)
}