首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift:带有UITableViewCellStyle.Subtitle的UITableViewCell中的多个detailTextLabel行

Swift:带有UITableViewCellStyle.Subtitle的UITableViewCell中的多个detailTextLabel行
EN

Stack Overflow用户
提问于 2016-06-25 22:11:59
回答 1查看 673关注 0票数 0

我有一台tableViewCell

代码语言:javascript
复制
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")

它具有以下设置:

代码语言:javascript
复制
    cell.textLabel?.font = UIFont.boldSystemFontOfSize(15.0)
    cell.textLabel?.text = titleString

    cell.detailTextLabel?.text = descriptionString
    cell.detailTextLabel?.numberOfLines = 0

descriptionString由一个超文本标记语言字符串组成。

我设置好了

代码语言:javascript
复制
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

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

但是,当detailTextLabel大于1行时,这会导致混乱的表视图显示。detailTextLabel将包装到下一个表视图单元格上。

这个问题的解决方案是什么?UITableViewAutomaticDimension似乎没有按预期工作。

EN

回答 1

Stack Overflow用户

发布于 2016-06-25 22:32:48

表格视图单元格中的多行标签

首先定义

代码语言:javascript
复制
private let useAutosizingCells = true

  override func viewDidLoad() {
        super.viewDidLoad()
        
        if useAutosizingCells && tableView.respondsToSelector(Selector("layoutMargins")) {
            tableView.estimatedRowHeight = 88
            tableView.rowHeight = UITableViewAutomaticDimension
        }
}

//标记:- UITableViewDataSource

代码语言:javascript
复制
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count
}
 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)     


        if useAutosizingCells && tableView.respondsToSelector(Selector("layoutMargins")) {
            cell.textLabel?.numberOfLines = 0
            cell.detailTextLabel?.numberOfLines = 0
        }
        
        return cell
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38029382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档