2016-04-14 61 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