2015-10-05 195 views
13

我有一個典型的主 - 細節應用程序,允許用戶瀏覽滾動的對象列表,然後鑽到任何特定對象的細節與push segue。滾動主列表是一個由原型單元構建的UITableView,細節場景是具有固定數量的部分和單元格的靜態UITableView。動態類型和自定義單元格與靜態表

我想在我的應用程序中實現Dynamic Type和Self-Sizing Cells,以便用戶可以更改基本字體大小。到目前爲止,我已經成功地使用原型單元滾動列表自定義單元格大小:通過使用自動佈局,將每個標籤中的行數設置爲0,並設置tableView.rowHeight = UITableViewAutomaticDimension,每個原型單元的高度增長或縮小以適應文字的大小。

但我無法在靜態表格視圖中實現相同的效果。無論我使用自定義單元格還是內置單元格類型,字體都會增大/縮小,但單元格高度不會。

所以我的問題實際上是兩個問題:1)是否有可能在靜態表視圖中實現自定義大小的單元格,就像我用我的原型表視圖一樣? 2)如果第一個問題的答案是否定的,我該如何編寫代碼來測量靜態表格視圖單元格中標籤的高度並適當調整單元格高度?

謝謝!

回答

1

我很感謝提供給我昨天發佈的問題的答案。不幸的是(可能是因爲我自己作爲iOS程序員的經驗不足),我無法使systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)方法工作。一些試驗和錯誤後,但是,我發現,似乎工作不同的方法:

import UIKit 

class MasterTVC: UITableViewController { 
    @IBOutlet weak var staticCustomCellLabel: UILabel! 

    //viewDidLoad 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.tableView.estimatedRowHeight = 44.0 

     staticCustomCellLabel.text = "This is the text for the static custom cell label; This is the text for the static custom cell label; This is the text for the static custom cell label" 

     //Register handleDynamicTypeChange in observer: self 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleDynamicTypeChange:", name: UIContentSizeCategoryDidChangeNotification, object: nil) 
    } 

    //deinit 
    deinit { 
     NSNotificationCenter.defaultCenter().removeObserver(self) 
    } 

    //handleDynamicTypeChange 
    //called when user changes text size in Settings > General > Accessibility > Larger Text 
    //or Settings > Display & Brightness > Text Size 
    func handleDynamicTypeChange(notification: NSNotification) { 
     staticCustomCellLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody) 

     self.tableView.reloadData() 
    } 

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

在此故事板側,設置始於一個表視圖控制器具有以下屬性:

  • 類別:MasterTVC(我的自定義類);
  • 指定初始視圖控制器;
  • 內容:Static Cells;
  • 風格:分組
  • 部分:1.

科-1:

  • 行數:1。

表格視圖單元格:

  • 風格:自定義(在這裏使用基本原因標籤改變設置文字大小時消失);
  • 包含UILabel。

所述的UILabel被進一步配置爲:

  • 字體:車身(動態型方式);
  • 行:0;
  • 固定在內容視圖的頂部,底部,左側和右側的所有四邊(我使用了相應的約束8,8,15,15);
  • 在自定義類代碼中帶有@IBOutlet。

這適用於我在Xcode 7和Swift 2中爲iOS 9設計的自動佈局功能。有趣的是,如果剝離用於立即響應文本大小變化的NSNotificationCenter代碼,自定義單元代碼基本上是兩行:在viewDidLoad中設置estimatedRowHeight,並從tableView:heightForRowAtIndexPath中返回UITableViewAutomaticDimension。

基於這些結果,我相信我原來的問題的答案是:1)是的;和2)請參閱1.

1

因爲這是一個靜態表,你不能出隊的細胞,所以你必須創建IBOutlets他們,我就抱着他們在這樣一個數組:

@IBOutlet var cells: [UITableViewCell]! 

然後你會必須實現heightForRowAtIndexPath和計算大小:

var rowSizes: [Int: CGFloat] = [:] 

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

func calculateHeightForCell(indexPath: NSIndexPath) -> CGFloat { 

    // check if we already calculated the height 
    if let height = rowSizes[indexPath.row] { 
     return height 
    } 

    // else, calculate it 
    let cell = cells[indexPath.row] 

    let size = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize) 

    rowSizes[indexPath.row] = size.height 

    return size.height 
} 

rowSizes就像一個高速緩存,這將持有該行高度,所以他們只會計算一次。更改字體大小時,只需清空rowSizes並刷新表格,以便再次進行計算。

15

靜態表視圖返回在界面構建器中設置的行高。 tableView.rowHeight設置似乎完全被忽略。這顯然是UIKit中的一個錯誤。

爲了解決這個問題,只需覆蓋-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath並返回UITableViewAutomaticDimension

+0

非常感謝! – mangerlahn

+0

你可以設置'tableView.rowHeight'而不是使用委託功能嗎? – BallpointBen

+0

@BallpointBen請在我的回答中閱讀第二句。 – Andy

3

首先,爲了使UITableViewAutomaticDimension工作,確保添加了所有的左,右,下,和相對於電池容器視圖頂部添加約束這兩功能,在你的類

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

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

二。另外不要忘記將標籤的行數設置爲零。

相關問題