开发者

how to resize UITableViewCell so it fits any detailTextLabel

im making a iphone app so that any text in the detailTextlabel will fit 开发者_开发技巧no matter what

for example...if the detailTextLabel is short it will show a normal size cell however it if is long it will show a bigger cell

thank you :D


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


    CGSize maxSize = CGSizeMake(max_size);
    CGSize cellSize = [yourString 
                       sizeWithFont:[UIFont systemFontOfSize:15]
                       constrainedToSize:maxSize
                       lineBreakMode:UILineBreakModeWordWrap];
    return cellSize.height;

}


Consider my implementation:

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

switch (theSection) {
    case 0:
        if (indexPath.row ==6) //the row you need to check for height (if any)

        {

            if([yourString length]<=35) // if less than 35 chars, just return a basic size of 50
            {
                return 50;
            }

            CGSize detailSize = [yourString sizeWithFont:[UIFont systemFontOfSize:20]constrainedToSize:CGSizeMake(270, 4000)lineBreakMode:UILineBreakModeWordWrap];

            return detailSize.height;
        }

hope it helps, to help your understanding you may want to have a look at 'sizeWithFont' to understand what this is doing to return a size.


You have to use the tableView:heightForRowAtIndexPath: method on your UITableViewDelegate to return the height of the row. Note that this method should be very fast if you have more than a few rows, as it will be called for every row in the table rather than just the visible rows.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜