Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableViewCell:获取单元格中UILabel的确切宽度_Ios_Swift_Uitableview_Uilabel - Fatal编程技术网

Ios UITableViewCell:获取单元格中UILabel的确切宽度

Ios UITableViewCell:获取单元格中UILabel的确切宽度,ios,swift,uitableview,uilabel,Ios,Swift,Uitableview,Uilabel,在自定义单元格中获取正确的标签宽度时出现问题。 这是我在故事板中的单元: 创建表时,我知道调用CellForRowatineXpath时,只是创建了单元格,还没有添加到UITableView,所以标签的宽度是304px。这会导致如下结果: 滚动后,单元格已添加到UITableView,以便正确显示,标签宽度为164px 在将单元格/标签添加到UITableView之前,是否有办法知道其确切宽度 标签上的约束:前导、尾随、顶部和高度 以下是源代码: func tableView(table

在自定义单元格中获取正确的标签宽度时出现问题。 这是我在故事板中的单元:

创建表时,我知道调用CellForRowatineXpath时,只是创建了单元格,还没有添加到UITableView,所以标签的宽度是304px。这会导致如下结果:

滚动后,单元格已添加到UITableView,以便正确显示,标签宽度为164px

在将单元格/标签添加到UITableView之前,是否有办法知道其确切宽度

标签上的约束:前导、尾随、顶部和高度

以下是源代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: ArticleCell = tableView.dequeueReusableCellWithIdentifier("articleCell", forIndexPath: indexPath) as! ArticleCell
    let item = array.objectAtIndex(indexPath.row) as! Article
    cell.showDataForArticle(item, dateFormatter: dateFormatter)
    return cell
}

func showDataForArticle(article: Article, dateFormatter: NSDateFormatter) {
    self.titleLbl.text = article.articleTitle
    titleHeightConstraint.constant = titleLbl.requiredHeight() <--- where problem happens

    self.thumbnailImgView.imageLink(article.articleThumbnailLink!)
    self.authorLbl.text = article.articleCreator
    self.dateLbl.text = dateFormatter.stringFromDate(article.articlePubDate!)
}


extension UILabel {
func requiredHeight() -> CGFloat {
    let frame = CGRectMake(0, 0, self.frame.size.width, CGFloat.max)
    let label: UILabel = UILabel(frame: frame)
    label.numberOfLines = 0
    label.lineBreakMode = .ByWordWrapping
    label.font = self.font
    label.text = self.text
    label.sizeToFit()

    return label.frame.size.height
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
让cell:ArticleCell=tableView.dequeueReusableCellWithIdentifier(“ArticleCell”,forindexath:indexath)作为!ArticleCell
将item=array.objectAtIndex(indexPath.row)设为!Article
cell.showDataForArticle(项,dateFormatter:dateFormatter)
返回单元
}
func showDataForArticle(文章:文章,日期格式化程序:NSDateFormatter){
self.titleLbl.text=article.articletTitle
TitleLightConstraint.constant=TitleBl.requiredHeight()CGFloat{
设frame=CGRectMake(0,0,self.frame.size.width,CGFloat.max)
let label:UILabel=UILabel(帧:帧)
label.numberOfLines=0
label.lineBreakMode=.ByWordWrapping
label.font=self.font
label.text=self.text
label.sizeToFit()
返回标签.frame.size.height
}

尝试在
func tableView(tableView:UITableView,
willDisplayCell单元格:UITableViewCell,

forRowAtIndexPath:nsindepath)
method

就试图获取标签的宽度而言,您是否尝试过在标签行中单独使用一些方法

self.YourLabelName.bounds.width

您可以拥有一个标签,该标签将根据使用自动布局显示的文本调整其高度

1.要使此功能正常工作,您不应设置单元格的静态高度,只需添加以下两个代理即可。

    -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 44;
    }

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       return UITableViewAutomaticDimension;
    }
2.您应该为单元格设置从上到下的约束, 在您的情况下,标题标签应该具有上空间到单元格、下空间到ImageView、下空间到单元格(标准间距)和下空间到AuthorLabel(标准间距)。

3.您想要的动态高度为文本的标签应已将行数设置为0,将LineBreakMode设置为换行。

就是这样,也可以找到与您的案例类似的链接


尝试对tableview单元格进行子类化,并在layoutsubviews方法或initWithStyle方法中查找边界。我建议您使用自动布局。显示cellforRow的代码,然后使用自动布局。当然,我使用了自动布局:)我已在上面添加了我的代码