2016-08-16 39 views
3

我正在爲列表製作一個視圖。它具有動態的字幕長度。所以我使用了UITableViewAutomaticDimension屬性。我沒有應用任何約束,因爲這兩個標籤都是表視圖單元格屬性,因此不需要創建新的自定義單元格。UITableViewAutomaticDimension應用於字幕表格視圖單元格不能正常工作

但運行這個項目後,我標記了UITableViewAutomaticDimension不能正常工作。我不知道我做錯了什麼。我的代碼是像下面

視圖做負載

@IBOutlet weak var tblabc: UITableView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     tblabc.separatorColor = UIColor.clearColor() 

     tblabc.estimatedRowHeight = 44 
     tblabc.rowHeight = UITableViewAutomaticDimension 



     // Do any additional setup after loading the view. 
    } 

表視圖委託和數據源

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 5 
    } 
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     var cell = tableView.dequeueReusableCellWithIdentifier("Cell") 
     if (cell == nil) 
     { 

      cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Cell") 
      cell?.selectionStyle = .None 
      cell?.detailTextLabel?.font = _SETREGULARFONT_KARLA(IS_IPAD ? 19:15) 
      cell?.textLabel?.font = _SETBOLDFONT_KARLA(IS_IPAD ? 17:13) 
      cell?.detailTextLabel?.textColor = getColorFromRGB(120, G: 132, B: 158, A: 1.0) 
      cell?.detailTextLabel?.numberOfLines = 0 
      cell?.detailTextLabel?.lineBreakMode = .ByTruncatingTail 
      cell?.selectionStyle = .None 

     } 

     cell?.textLabel?.text = "Testing" + "\(indexPath.row)" 
     cell?.detailTextLabel?.text = "tesfhjdsfskdfhjkfhdjskfhsjkdfhdjsxdfgfjljkgjfklgfhsdjfhjkdshfdjskfhdjfjkfhafyhdsifjhdjksfbshdjkfkhdksjfhdjsfyhds8ufdhsfjkhdsfjhdfudsiufhdsfh" 
     cell?.imageView?.image = UIImage(named: "new_temp") 

     return cell! 
    } 

查看看起來像下面的圖片

enter image description here

+0

請參閱下面的鏈接http://stackoverflow.com/questions/36587126/autolayout-ignores-multi-line-detailtextlabel-when-calculating-uitableviewcell-h –

回答

1

選中此修改代碼: -

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 5 
} 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") 
    if (cell == nil) 
    { 

     cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Cell") 
     cell?.selectionStyle = .None 
     cell?.detailTextLabel?.font = _SETREGULARFONT_KARLA(IS_IPAD ? 19:15) 
     cell?.textLabel?.font = _SETBOLDFONT_KARLA(IS_IPAD ? 17:13) 
     cell?.detailTextLabel?.textColor = getColorFromRGB(120, G: 132, B: 158, A: 1.0) 
     cell?.detailTextLabel?.numberOfLines = 0 
     cell?.detailTextLabel?.lineBreakMode = .ByTruncatingTail 
     cell?.selectionStyle = .None 

    } 

    cell?.textLabel?.text = "Testing" + "\(indexPath.row)" 
    cell?.detailTextLabel?.text = "tesfhjdsfskdfhjkfhdjskfhsjkdfhdjsxdfgfjljkgjfklgfhsdjfhjkdshfdjskfhdjfjkfhafyhdsifjhdjksfbshdjkfkhdksjfhdjsfyhds8ufdhsfjkhdsfjhdfudsiufhdsfh" 
    cell?.imageView?.image = UIImage(named: "new_temp") 

    return cell! 
} 

請刪除HeightForRowAtIndex數據源方法。

-1

試試這個代碼:

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

刪除這兩個行:

tblabc.estimatedRowHeight = 44 
tblabc.rowHeight = UITableViewAutomaticDimension 
+0

感謝您的快速回復,但這是行不通的 –

+0

The estimatedRowHeight不應該是UITableViewAutomaticDimension,它應該是一個實數,以幫助系統在自動佈局確定動態行高度之前計算行高的估計值。 –

0

問題應固定在iOS版11.快捷的解決方案致力於降低就好了版本是通過設置其textlabel.attributedText具有更大膽的第一行(s)+ \ n +常規更多的行來在基本單元格內僞造期望的字幕外觀。

相關問題