Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.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高度以适合字符串_Ios_Uitableview - Fatal编程技术网

Ios 计算UITableViewCell高度以适合字符串

Ios 计算UITableViewCell高度以适合字符串,ios,uitableview,Ios,Uitableview,我有一个NSMutableArray,它可以包含数百个不同的字符串。每个字符串都是用户定义的,可以有任意长度,但不能超过一个平均段落 我需要知道每根线能走多少行。因此,我可以计算每个UITableviewCell所需的高度使用以下代码计算和设置单元格的高度: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // Define `veryvery

我有一个
NSMutableArray
,它可以包含数百个不同的字符串。每个字符串都是用户定义的,可以有任意长度,但不能超过一个平均段落


我需要知道每根线能走多少行。因此,我可以计算每个
UITableviewCell所需的高度

使用以下代码计算和设置单元格的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Define `veryverySmallTitleFont`
    // Define `descLabel`

    NSString *ourText = @"Your string data from array";
    UIFont *font = [UIFont fontWithName:@"Verdana" size:veryverySmallTitleFont];
    font = [font fontWithSize:veryverySmallTitleFont];

    CGSize constraintSize = CGSizeMake(descLabel.frame.size.width, 1000);
    CGSize labelSize = [ourText sizeWithFont:font
                           constrainedToSize:constraintSize
                               lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height;
}

我个人使用一种方便的方法,然后在
tableView:heightforrowatinexpath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self cellHeightForRowAtIndexPath:indexPath];
}

- (CGFloat)cellHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellString = <YOUR MODEL OBJECT>;

    NSDictionary *attributes = @{ NSFontAttributeName: [UIFont systemFontOfSize:16.0f] };
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
                                                   initWithString:cellString
                                                   attributes:attributes];

    CGFloat width = self.tableView.frame.size.width - 32.0f;
    CGRect frame = [attributedString boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                    options:NSStringDrawingUsesLineFragmentOrigin context:nil];

    // Add some extra padding to the height
    CGFloat height = frame.size.height + 16.0f;

    return ceil(height);
}
-(CGFloat)tableView:(UITableView*)tableView rowatinexpath的高度:(nsindepath*)indepath
{
return[self-cellheightforrowatinexpath:indexPath];
}
-(CGFloat)rowatinexpath的单元格高度:(nsindepath*)indepath
{
NSString*cellString=;
NSDictionary*attributes=@{NSFontAttributeName:[UIFont systemFontOfSize:16.0f]};
NSMUTABLeAttributeString*AttributeString=[[NSMUTABLeAttributeString alloc]
initWithString:cellString
属性:属性];
CGFloat width=self.tableView.frame.size.width-32.0f;
CGRect frame=[attributedString boundingRectWithSize:CGSizeMake(宽度,MAXFLOAT)
选项:NSStringDrawingUserLineFragmentOrigin上下文:nil];
//在高度上添加一些额外的填充
CGFloat高度=框架尺寸高度+16.0f;
返回天花板(高度);
}

当然,我在
UITableViewCell
中将我的
UILabel
numberOfLines
属性设置为
0
,你能在
UITableViewCell
中使用
UITextView
吗?什么是
NSMutable*
?我看不出原因,我改变字体大小、字体、文本颜色,背景色道具,我也可以在textview上使用。抱歉,自动更正决定NSMutableArray不存在,但NSMutable不存在。请参见
UITextView
我看到此选项:sizeWithFont:constrainedToSize:lineBreakMode现在不推荐使用。