开发者

resizable UITableViewCell

开发者 https://www.devze.com 2023-02-12 01:53 出处:网络
Is it possible to create a custom UITableViewCell that resizes depending on the length of the text that I want to put into? If p开发者_运维技巧ossible, how do I do this?@EquinoX yes it is possible thr

Is it possible to create a custom UITableViewCell that resizes depending on the length of the text that I want to put into? If p开发者_运维技巧ossible, how do I do this?


@EquinoX yes it is possible through heightForRowAtIndexPath delegate....Please have a look on this and Dynamic Height UITableViewCell they have same thing you are asking.

Good Luck!


Absolutely,

I like to set all the data on my cell by passing it a model (just an NSObject with data in it). Then I create a custom cell that can have its data set by the model, and I add a class level function to return the size. It looks like this.

TableViewDelegate:

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    XTableViewCellModel *model = [self modelForIndexPath:indexPath];
    return [XTableViewCell cellHeight:model];
}

XTableViewCell:

+(CGFloat)cellHeight:(XTableViewCellModel*)model {
    CGSize titleSize = [model.title sizeWithFont:model.titleFont
                         constrainedToSize:CGSizeMake(280, 9999)
                             lineBreakMode:UILineBreakModeTailTruncation];
    return titleSize.height;
}

Note that setting lineBreakMode to UILineBreakModelTailTruncation only takes effect after the height of 9999 points is reached. Until then the text wraps normally.

Check out my open source framework around this stuff. It has a lot of resizable cell types built by default: https://github.com/andrewzimmer906/XCell


Look at this Sample Code it uses exactly what you looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号