簡體   English   中英

當我從一個我猜到的視圖控制器返回時,為什么我的UITableView的格式完全出錯?

[英]Why does my UITableView's formatting go completely awry when I return from a view controller I segued to?

我有一個帶有自定義單元格的UITableView ,其中有一些標簽可以動態決定單元格的高度。 當我點擊一個單元格並切換到一個新的視圖控制器時,返回所有格式的單元格完全搞砸了,我無法弄清楚是什么導致它。

這是細胞通常的樣子:

在此輸入圖像描述

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

當我回到這個表視圖時,它看起來像這樣:

在此輸入圖像描述

我無法弄清楚導致它布局的原因與我離開時不同。 如果我滾動它似乎“重置”它們回到它們應該是什么,但在初始加載時它們真的搞砸了。 如果需要,我可以附加項目,但在故事板之外真的不多。

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

當從segue返回時,表視圖單元格似乎沒有根據內容調整大小。 使用示例項目,我在viewWillAppear中拋出了重新加載數據,這似乎解決了這個問題。

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

您的項目實際上存在幾個問題。

數據加載和自動布局

第一個是在使用數據繪制單元格時導致奇怪的行為。 從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

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

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

我自己也遇到過類似的問題。 我下載了你的項目,似乎我通過刪除和調整一些約束來解決它。 這就是我的約束現在的樣子:

在此輸入圖像描述

我也將此添加到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)
    }
}

現在你甚至可以旋轉設備並刪除行,這一切都很棒!


但是,如果您在導航控制器上推送此視圖仍然存在問題(這是我的問題在開始時)。 請參閱下面的故事板以獲得一些時髦的標簽:

在此輸入圖像描述

為了解決這個問題,我們實際上似乎必須做一個黑客攻擊! (該死你,蘋果,這是怎么回事?!)

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
        }
    }
}

目前,我認為這是最好的。

由於方法tableView:estimatedHeightForRowAtIndexPath將在每次轉換到新的MVC時被調用,並且更改autolayout,你可以做

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

重用自動布局

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM