當我在tabeview中選擇多個單元格時,也選擇了視圖外的單元格。我明白這是因爲我重複使用了單元格,並且在向下滾動時保持其選擇。我發現有幾個人有類似的問題,但不能將他們的解決方案轉化爲解決我的問題。我曾嘗試沒有dequeing一個單元格,只需使用:多選UITableView無dequeueReusableCellWithIdentifier
let cell = NewBillSplitterItemCell()
,但得到:
unexpectedly found nil while unwrapping an Optional value
就行了:
cell.currentSplitters.text = splitterList
下面的代碼
:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
fetchBillItems()
let cell: NewBillSplitterItemCell = tableView.dequeueReusableCellWithIdentifier("NewBillSplitterItemCell") as! NewBillSplitterItemCell
let item = allItems[indexPath.row]
let numberOfSplitters = item.billSplitters?.count
if numberOfSplitters == 0 {
cell.currentSplitters.text = "No one is paying for this item yet."
} else {
var splitterList = "Split this item with "
let itemSplitters = item.billSplitters?.allObjects as! [BillSplitter]
for i in 0...Int((numberOfSplitters)!-1) {
if numberOfSplitters == 1 {
splitterList += "\(itemSplitters[i].name!)"
} else {
splitterList += ", \(itemSplitters[i].name!)"
}
}
cell.currentSplitters.text = splitterList
}
cell.name.text = item.name
cell.price.text = "£\(item.price!)"
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
if cell.accessoryType == .Checkmark
{
cell.accessoryType = .None
selectedItems.removeAtIndex(selectedItems.indexOf(allItems[indexPath.row])!)
} else {
cell.accessoryType = .Checkmark
selectedItems.append(allItems[indexPath.row])
}
}
}
我不太明白要做什麼和任何hel p會很棒。由於
單元格內的所有內容都是零,所以它從cell.currentSplitters開始,然後cell.name會得到相同的錯誤,如果它得到那麼遠等 – Wazza
什麼是在'cell.currentSpliiters.text''currentSplitters'?它是一個UILabel嗎? –
是的,那麼.name和.price – Wazza