UITableViewCell风格字幕多行无法正常工作

时间:2016-11-09 05:20:40

标签: ios uitableview swift2

我有一个自定义样式UITableviewCell,并且它有其他的默认高度我正在使用带有字幕样式的tableview单元格,我想将副标题设置为多行。并且它做得很好,但是在计算tableview单元格高度方面存在问题,我使用UITableviewAutomaticDimension但它不起作用。
这是我的代码

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

1 个答案:

答案 0 :(得分:0)

你需要编写的viewDidLoad()方法中的

 //add this in your add button click handler
 $tinput = $('<input type="text" name="bootstrap" class="typeahead"/>');

 //apply bootstrap typeahead
 $tinput.typeahead();

再添加一个方法

yourTableViewName.rowHeight = UITableViewAutomaticDimension

我希望这会对你有所帮助