2016-04-14 32 views
-1

我有一个问题,我不明白,因为我是新的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子句中,但随后其他奇怪的事情发生。

+0

如果你想确定哪个按钮在tableview中被按下,我建议你设置button.tag = indexPath.row ...这样你就可以确切地知道你按下了什么按钮,而没有这个可怕的事情......设置标签在cellForRowAtIndexPath –

回答

3

请勿使用'!'。

cellForRowAtIndexPath返回UITableViewCell?并且如果该行在您拨打电话时不可见,则可以为零。在将它转换为预期的对象类型之前,请检查您是否真的使用了if let

+0

好吧,但行怎么可能不可见?如果我改变并使用,如果让我们选择一些行并按下delete,它就会起作用。 tableview更新,只显示剩余的结果。但是,当我选择一个新结果并删除它时,所有表格行都变为空白。 –

+0

我不知道您的具体情况,但是,例如,如果您在用户界面中一次显示七行,并且有十个结果,那么三个或那些结果没有关联的单元格。 (您可能需要输出从'numberOfRowsInSection'返回的实际行数。) –

0

有几件事情怎么回事,让我们逐一时间

  1. 的崩溃是迫使通过!nil值部分应该是显而易见的
  2. for循环从去0直到行数,但UITableViewCell实例得到回收,因此无法工作
  3. 该复选框选择不是标准的iOS表格单元格选择,而是更自定义的一些

如果您正在使用的UITableView的内置的选择行为,你可以使用这个方法:如果你想继续使用您的自定义复选框选择

iOS, How to find selected row Of UITableView?

,可能比做到这一点的最简单的方法将听取选择更改,并在选择事物时建立NSIndexPath。之后,当按下删除按钮时,您将有一个预先建好的索引准备删除,您将不必循环搜索所选内容。

+0

感谢您提出的宝贵意见!据我了解,标准功能是为整行选择?我想如果想删除它,只需按下按钮就更直观了? –

+0

选择一行或多行。 “NSIndexPath”可以包含多个内容https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/ – slf