2016-11-09 109 views
0

我有一個自定義樣式UITableviewCell它有其他默認高度我使用tableview單元格與字幕樣式,我想使字幕成爲多行。它很好,但有計算tableview單元格高度的問題,我使用UITableviewAutomaticDimension但它不工作。
這裏是我的代碼UITableViewCell樣式字幕多行不工作

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if(indexPath.row==0){ 
     var cell: ImageCell! = tableView.dequeueReusableCellWithIdentifier("ImageCell") as? ImageCell 
     if cell == nil { 
      tableView.registerNib(UINib(nibName: "ImageCell" ,bundle: nil), forCellReuseIdentifier: "ImageCell") 
      cell = tableView.dequeueReusableCellWithIdentifier("ImageCell") as? ImageCell 
     } 
     return cell 
    }else{ 

     var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("SubtitleCell") 
     if (cell == nil) { 
      cell = UITableViewCell.init(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "SubtitleCell") 
      cell!.backgroundColor = UIColor.brownColor() 
      cell!.textLabel?.font = UIFont.init(name: "HelveticaNeue", size: 15) 
      cell!.textLabel?.textColor = UIColor.blackColor() 
      cell!.textLabel?.highlightedTextColor = UIColor.lightGrayColor() 
      cell!.selectedBackgroundView = UIView.init() 
     } 
     cell!.textLabel?.text = "TExt" 
     cell!.detailTextLabel!.text="Keep in mind that UITableView is defined as an optional in the function, which means your initial cell declaration needs to check for the optional in the property. Also, the returned queued cell is also optional, so ensure you make an optional cast to UITableViewCell. Afterwards, we can force unwrap because we know we have a cell." 
     allowMultipleLines(cell!) 
     cell!.imageView?.image = UIImage(named: "IconProfile") 

     return cell! 
    } 
} 
func allowMultipleLines(tableViewCell:UITableViewCell) { 
    tableViewCell.detailTextLabel?.numberOfLines = 0 
    tableViewCell.detailTextLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping 
} 
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if(indexPath.row==0){ 
     return 172 
    }else{ 
     return UITableViewAutomaticDimension 
    } 
} 

result

+0

可能的複製http://stackoverflow.com/questions/36587126/autolayout-ignores-multi-line-detailtextlabel-when-calculating-uitableviewcell-h –

回答

0

在您的viewDidLoad()方法,你需要寫

yourTableViewName.rowHeight = UITableViewAutomaticDimension 

並添加一個方法

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if(indexPath.row==0){ 
     return 172 
    }else{ 
     return UITableViewAutomaticDimension; 
    } 
} 

我希望這將幫助你

+0

沒有兄弟它不工作結果仍然一樣。 –

+0

你正在使用自動佈局? –

+0

是自動佈局被選中。 –