我有一个问题,我不明白,因为我是新的iPhone应用程序编程和Swift。发现零当试图从表格单元格读取
我有一个Swift中的TableView,我用它来显示一些结果。我为每个单元格添加了一个按钮,以便用户可以选择不同的单元格,然后按删除键以删除单元格中显示的结果。
只有少数的结果工作得很好,但现在我已经开始得到零例外。
当我尝试获取特定行的单元格时,程序在函数getIndexToDelete中崩溃。
这里就是我处理表的代码:
import UIKit
class DailyResultTable: UIViewController, UITableViewDataSource, UITableViewDelegate {
var results = Results()
var yearShownIndex = 0
var monthShownIndex = 0
var dayShownIndex = 0
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var DeleteButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// self.view.backgroundColor = UIColor(patternImage: UIImage(named: "StatisticBackground")!)
}
// DataSource
func numberOfSectionsInTableView(tableview: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return (self.results?.getDayList(yearShownIndex, posMonth: monthShownIndex).count)!
print("return number of rows")
if (results.getYearList().count > 0){
if (results.getMonthList(yearShownIndex).count > 0){
if (results.getDayList(yearShownIndex, posMonth: monthShownIndex).count > dayShownIndex){
return (results.getDayList(yearShownIndex, posMonth: monthShownIndex)[dayShownIndex].results.count)
}
}
}
print("No numbers to show return 0")
return 0;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("DayResultCell", forIndexPath: indexPath) as! ResultTableCell
let row = indexPath.row
//cell.ResultField.text = String((results?.getDayList(yearShownIndex, posMonth: monthShownIndex)[row].day)!) + "/" + String((results?.getMonthList(yearShownIndex)[monthShownIndex].month)!)
let res = results.getDayList(yearShownIndex, posMonth: monthShownIndex)[dayShownIndex].results[row].result
let maxRes = results.getDayList(yearShownIndex, posMonth: monthShownIndex)[dayShownIndex].results[row].maxresult
let discipline = results.getDayList(yearShownIndex, posMonth: monthShownIndex)[dayShownIndex].results[row].discipline
let text1 = String(res) + "/"
let text2 = String(maxRes)
let text3 = " - " + discipline
let text = text1 + text2 + text3
print(text)
cell.ResultField.text = text
return cell
}
// Delegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
@IBAction func CheckBox(sender: UIButton) {
let image = UIImage(named: "Selected") as UIImage!
let selected = sender.selected
sender.selected = !selected
sender.setImage(image, forState: .Selected)
}
func getIndexToDelete()->[Int]{
var indices = [Int]()
for i in 0..<tableView.numberOfRowsInSection(0){
let indexPath = NSIndexPath(forRow: i, inSection: 0)
// Here does the program crash
let cell = tableView.cellForRowAtIndexPath(indexPath) as! ResultTableCell
if (cell.CheckBoxButton.selected){
indices += [i]
}
}
return indices
}
@IBAction func DeletePressed(sender: UIButton) {
let deleteIndices = getIndexToDelete()
var goback = false;
var count = 0;
for index in deleteIndices{
print("Count: " + String(count))
results.ListResults[yearShownIndex].months[monthShownIndex].day[dayShownIndex].results.removeAtIndex(index-count)
count += 1
print(String((results.getDayList(yearShownIndex, posMonth: monthShownIndex)[dayShownIndex].results.count)));
}
loadView()
results.recreatePlainResult()
results.saveResults()
if (results.ListResults[yearShownIndex].months[monthShownIndex].day[dayShownIndex].results.count == 0){
print(String(results.ListResults[yearShownIndex].months[monthShownIndex].day.count))
results.ListResults[yearShownIndex].months[monthShownIndex].day.removeAtIndex(dayShownIndex)
results.recreatePlainResult()
results.saveResults()
print(String(results.ListResults[yearShownIndex].months[monthShownIndex].day.count))
goback = true
}
if (results.ListResults[yearShownIndex].months[monthShownIndex].day.count == 0){
results.ListResults[yearShownIndex].months.removeAtIndex(monthShownIndex)
results.recreatePlainResult()
results.saveResults()
goback = true
}
if (results.ListResults[yearShownIndex].months.count == 0){
results.ListResults.removeAtIndex(monthShownIndex)
results.recreatePlainResult()
results.saveResults()
goback = true
}
if (goback){
// return;
navigationController?.popViewControllerAnimated(true)
}
}
}
这里是ResultTableCell:
import UIKit
class ResultTableCell: UITableViewCell {
@IBOutlet weak var ResultField: UITextField!
@IBOutlet weak var CheckBoxButton: UIButton!
}
我当然可以把let cell = tableView.cellForRowAtIndexPath(indexPath) as! ResultTableCell
一个if子句中,但随后其他奇怪的事情发生。
如果你想确定哪个按钮在tableview中被按下,我建议你设置button.tag = indexPath.row ...这样你就可以确切地知道你按下了什么按钮,而没有这个可怕的事情......设置标签在cellForRowAtIndexPath –