Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.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
Objective c 动态更改UITableViewCell的高度_Objective C_Cocoa Touch_Uitableview - Fatal编程技术网

Objective c 动态更改UITableViewCell的高度

Objective c 动态更改UITableViewCell的高度,objective-c,cocoa-touch,uitableview,Objective C,Cocoa Touch,Uitableview,我试图在我的TableView中显示每个用户的评论。评论可以通过一个图像来容纳 通常,我们可以将UITableViewCell的高度设置为Rowatinex的高度。但是,我希望每个单元格根据聊天和图像(如果包括图像)扩展到图像高度 如何根据UITableViewCell的内容增加其高度?您仍然需要在heightForRowAtIndex:中进行此操作,但必须动态确定高度 我正在设想这样的事情: - (CGFloat)tableView:(UITableView *)tableView heigh

我试图在我的TableView中显示每个用户的评论。评论可以通过一个图像来容纳

通常,我们可以将UITableViewCell的高度设置为Rowatinex的高度。但是,我希望每个单元格根据聊天和图像(如果包括图像)扩展到图像高度


如何根据UITableViewCell的内容增加其高度?

您仍然需要在heightForRowAtIndex:中进行此操作,但必须动态确定高度

我正在设想这样的事情:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommentModel *comment = self.comments[indexPath.row];
    CGFloat height = [CommentTableViewCell heightForComment:comment];
    return height;
}
然后在CommentTableViewCell中,有一个类方法,类似于:

// Calculates the height a CommentTableViewCell should be when configured with the given comment
+ (CGFloat)heightForComment:(CommentModel *)comment {
    // This part of the code will be very specific to your own project
    CGFloat height = BASE_HEIGHT;
    height += comment.image.size.height;
    height += comment.message.size.height;
    return height;
}

我知道此代码可能与您的代码库不匹配,但它应该让您大致了解实现目标的方法。

如果您没有自定义单元格类,请使用此代码。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

         float PADDING = 0.5f; // USE FOR DIFFERNCE BETWEEN YOUR ENDING TEXT AND SEPERATOR
         UIImage *image =[UIImage imageNamed:@"test.png"]; // Some Image you want to put dynamically.
         NSString *text = @"Sometext That you want tro fill Dynamically";

         CGSize textSize = [text sizeWithFont:[UIFont boldSystemFontOfSize:14.0f] constrainedToSize:CGSizeMake(270 , 1000)];
         return ((textSize.height+image.size.height+ PADDING * 3) + 30);
}

如果您有自定义类,则必须将HIGHT传递给自定义单元格,以便在此处展开单元格时管理标签HIGHT。您可以在CELL类中单独执行此操作,但要将其与实际的hight F CELL同步。设置自定义类的值时,必须通过HIGHT。

您可以为注释添加新行。。。比增加行高更好。。。您甚至可以将一个分区中的每个用户及其注释设置为该分区中的行。问题是,我们将在TableView的cellForRowAtIndex委托函数中设置单元格值。但heightForRow是第一个打电话的。“我怎么能做到这一点呢。@穆罕默德·马杜马希望你没有试图动态更改CellForRowatineXpath中的高度。如果您有一个合理的设计,那么获取用于在CellForRowatineXpath:中设置单元格数据的相同数据并使用它计算HeightForRowatineXpath:中的高度应该是很简单的。我建议将计算代码放在一个单独的视图类中,以保持无关逻辑的分离。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

         float PADDING = 0.5f; // USE FOR DIFFERNCE BETWEEN YOUR ENDING TEXT AND SEPERATOR
         UIImage *image =[UIImage imageNamed:@"test.png"]; // Some Image you want to put dynamically.
         NSString *text = @"Sometext That you want tro fill Dynamically";

         CGSize textSize = [text sizeWithFont:[UIFont boldSystemFontOfSize:14.0f] constrainedToSize:CGSizeMake(270 , 1000)];
         return ((textSize.height+image.size.height+ PADDING * 3) + 30);
}