2016-04-28 77 views
0

所以我設置一個UITableViewCell的佈局編程時,它被選中:斯威夫特 - 編程設定的UITableViewCell復位上滾動

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    self.selectedCellIndexPath = indexPath 
    var selectedCell = tableView.cellForRowAtIndexPath(indexPath)! 
    tableView.beginUpdates() 
    tableView.endUpdates() 
    var cell:SelectedPatientCell = tableView.dequeueReusableCellWithIdentifier("patient selected", forIndexPath: indexPath) as! SelectedPatientCell 
    cell.patientName.text = patients[indexPath.row].fullName 
    cell.dob.text = patients[indexPath.row].dob 
    ... 
    selectedCell = cell 
} 

當我滾動的tableView,細胞復位的佈局到原來的佈局設置在cellForRowAtIndexPath中。但是,當我在上面的函數中設置高度時,高度保持不變。有誰知道如何解決這一問題?

這裏是發生了什麼事的專輯: http://imgur.com/a/OUIMJ

圖片1:初始狀態

圖片2:選中狀態(應該如何留在滾動)

圖3:到底發生了什麼

+0

這是不清楚問題是什麼。你能否更詳細地解釋你想要完成什麼,出了什麼問題?你選擇的時候想改變單元嗎? – ocwang

+0

所以一個單元有兩個狀態,未被選中和選擇。當它被選中並滾動時,它會重置爲原始佈局。我會看看我是否可以採取一些截圖來使其更清楚。 – nerras

回答

0

所以我發現我自己的解決方案: 而不是做

tableView.beginUpdates() 
    tableView.endUpdates() 

我需要在函數的末尾要做到這一點:

tableView.reloadData() 

和解決問題

1

你應該持有

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    if indexPath == self.selectedCellIndexPath { 
     var cell:SelectedPatientCell = tableView.dequeueReusableCellWithIdentifier("patient selected", forIndexPath: indexPath) as! SelectedPatientCell 
     cell.patientName.text = patients[indexPath.row].fullName 
     cell.dob.text = patients[indexPath.row].dob 
     return cell 
    } 
    let cell = tableView.dequeueReusableCellWithIdentifier("patient selected") as! OriCell 
    ... 
    return cell 
} 
這種狀態

這樣如果你滾動tableView,它將不會恢復到原來的Cell。 希望很明顯。

+0

用示例模型向用戶解釋更多信息,以便後面所有人都可以參考 – Shubhank

+0

我重置我的答案,請選擇此答案 – xiaoming

+0

此解決方案對我無效 – nerras