2014-10-28 39 views
3

我有一個UITableView與自定義單元格,其中有幾個標籤,它動態地決定單元格的高度。當我點擊一個單元格並繼續到一個新的視圖控制器時,返回單元格的所有格式完全搞砸了,我無法弄清楚是什麼導致它。當我從一個視圖控制器返回時,爲什麼我的UITableView的格式化完全出錯?

這裏是細胞正常的樣子:

enter image description here

而且我對他們設置了一些非常基本的約束條件。頂部標籤固定在頂部和左邊距,並且從右側始終應大於等於20。其他標籤與第一個標籤的左側對齊,並在所有標籤之間設置垂直間距。中間標籤對邊距有一個正確的間距約束,並且底部標籤與第一個標籤的基線對齊,並且它們之間具有水平間距。

當我Segue公司回到這個表中查看它看起來像這樣但是:

enter image description here

我想不出是什麼原因造成佈局比我離開的時候是不同的。如果我滾動它似乎「重置」他們回到他們應該是,但在最初的負載他們真的搞砸了。如果需要,我可以附加該項目,但在Storyboard之外確實沒有太多東西。

cellForRowAtIndexPath:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTableViewCell 

    let object = objects[indexPath.row] 

    cell.title1.text = object.name 
    cell.title2.text = object.color 
    cell.title3.text = object.roar 

    return cell 
} 

示例項目:http://cl.ly/040L2z0q0V2d

+0

您可能還沒有使用的'deQueueReusableCellWithIdentifier'。試試你的'cellForRowAtIndexPath'。 – 2014-10-28 14:11:32

+0

要添加到Libran編碼器的建議,使用dequeueReusableCellWithIdentifier:atIndexPath: – 2014-10-28 14:17:09

+0

我相信我做這兩個。我已將我的'cellForRowAtIndexPath'方法添加到主文章中。 – 2014-10-28 14:20:11

回答

3

看來,從SEGUE返回時表視圖細胞沒有尺寸調整基礎上的內容。使用示例項目,我在viewWillAppear中拋出了一個重載數據,這似乎解決了這個問題。

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 
    self.tableView.reloadData() 
} 
2

實際上有幾個問題與您的項目。

數據加載自動版式

第一個引起數據繪製單元格時的奇怪行爲。從segue展開時,由於模糊的佈局計算,您會在桌面上看到這些額外的單元格。

:將數據分成override func viewWillAppear(animated: Bool) {並執行tableView.reloadData()(如正確地@rFessler建議)

另一方面,Autolayout是一種火熱的野獸。 Tamable。這是值得進一步調查的話題。我無法通過自動調整單元格高度使您的佈局工作,但我將爲您留下幾個參考和項目。


參考文獻:

http://www.appcoda.com/self-sizing-cells/

http://captechconsulting.com/blog/tyler-tillage/ios-8-tutorial-series-auto-sizing-table-cells


項目: http://cl.ly/3z3a2Z3a3U2K

1

我玩過這個,找到一個簡單的解決方案,添加這似乎解決了這個問題。

override func viewWillDisappear(animated:Bool) { 
    super.viewWillDisappear(animated) 
    self.tableView.estimatedRowHeight = 166.0 
} 
1

我自己也有類似的問題。我下載了你的項目,似乎我已經通過刪除和調整一些限制來解決它。這是我現在的約束如何看待:

enter image description here

而且我已經添加了這viewDidLoad中:

self.tableView.estimatedRowHeight = 120 
self.tableView.rowHeight = UITableViewAutomaticDimension 

我還添加了這個測試刪除:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete 
    { 
     self.objects.removeAtIndex(indexPath.row) 

     self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 
    } 
} 

現在你甚至可以旋轉設備和刪除行,這一切都工作出色!


但是,如果您在導航控制器上推送此視圖(這是我一開始就遇到的問題),仍然存在問題。看到我的故事板下面讓一些時髦的標籤:

enter image description here

爲了解決這個問題,看來我們確實需要做的一劈! (你這該死的蘋果,到底是怎麼回事這個?!)

var firstAppearance=true 
override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 
    if firstAppearance 
    { 
     if let indexPaths = self.tableView.indexPathsForVisibleRows() 
     { 
      self.tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.None) 
      self.firstAppearance = false 
     } 
    } 
} 

此刻,我覺得這是好得不能再好。

0

由於該方法tableView:estimatedHeightForRowAtIndexPath都會被調用你Segue公司到一個新的MVC,並更改自動版式的時候,你可以做

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 

重用自動版式

相關問題