2015-03-31 88 views
0

我一直在努力解決這個問題。我可以在標籤單元之間自由滾動,因爲它實際上可以記住它們。但是,如果我將描述單元從我的視圖中移出,它會立即將其從內存中移除並且無法恢復。相反,當我回滾到描述時,我只是得到「致命錯誤:意外地發現零,同時展開可選值」。所以,我有以下的代碼片段:Swift - 滾動備份時TableView崩潰

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(true) 
    tableView.delegate = self 
    tableView.dataSource = self 
    tableView.rowHeight = UITableViewAutomaticDimension 
    tableView.estimatedRowHeight = 44.0 
    tableView.reloadData() 
} 

我不知道,如果是viewWillAppear中在這種情況下,任何重要的,但如果它是那麼告訴我。無論如何,這是在我的表視圖中的細胞填充:

func GetDescription(cell:descCell, indexPath: NSIndexPath) { 
    cell.descText.text = descriptTextTwo.htmlToString 
} 

func GetTagCell(cell:basicTag, indexPath: NSIndexPath) { 
    let item = tagResults[indexPath.row]! 
    cell.titleLabel.text = item["tagname"]?.htmlToString 
} 

func GetValueCell(cell: basicTag, indexPath: NSIndexPath) { 
    let item = tagResults[indexPath.row]! 
    cell.valueLabel.text = item["value"]?.htmlToString 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if filledDescription == false { 
     return getDescriptionAtIndexPath(indexPath) 
    } else { 
     return getTagAtIndexPath(indexPath) 
    } 
} 

func getDescriptionAtIndexPath(indexPath:NSIndexPath) -> descCell { 
    let cell = self.tableView.dequeueReusableCellWithIdentifier(descriptionCell) as descCell 
    GetDescription(cell, indexPath: indexPath) 
    filledDescription = true 
    return cell 
} 

func getTagAtIndexPath(indexPath: NSIndexPath) -> basicTag { 
    let cell = self.tableView.dequeueReusableCellWithIdentifier(tagCell) as basicTag 
    GetTagCell(cell, indexPath: indexPath) 
    GetValueCell(cell, indexPath: indexPath) 
    return cell 
} 

那麼,怎樣才能讓我想起雨燕是什麼在第一個單元格?因爲我猜測這就是發生了什麼事,一旦你將它從視圖中移出,它就會刪除第一個單元格中的內容。我猜我必須用「indexPath」做些什麼,但我不確定如何在這種情況下實現它,如果我很遠,請告訴我我做錯了什麼。謝謝!

+0

對於哪一行你會得到錯誤? – giorashc 2015-03-31 12:52:48

+0

我沒有收到錯誤。該應用程序構建並啓動。直到我看到這個視圖並開始向下滾動,然後備份應用程序崩潰。 – 2015-03-31 12:55:13

+0

但是,如果您的設備是通過Xcode啓動的,您應該看到一個崩潰日誌,並在跟蹤發生之前爲您提供執行的最後一行......您是否看到一個? – giorashc 2015-03-31 12:59:02

回答

1

更改如下:

if filledDescription == false { 
    return getDescriptionAtIndexPath(indexPath) 
} else { 
    return getTagAtIndexPath(indexPath) 
} 

有了:

if indexPath.row == 0 { 
    return getDescriptionAtIndexPath(indexPath) 
} else { 
    return getTagAtIndexPath(indexPath) 
} 

這將確保該表中的第一個單元格將作爲「說明」細胞總是處理。由於filledDescription永遠不會成爲你的設置它true,當你回到那裏其實是可重複使用的單元格中包含「描述」它被視爲一個「變量」細胞(由於if線)的第一個單元格後false單元格數據

+1

謝謝。這工作完美! – 2015-03-31 13:28:21