现在的位置: 首页 > 综合 > 正文

iOS – UITableViewCell自适应高度

2013年12月07日 ⁄ 综合 ⁄ 共 1133字 ⁄ 字号 评论关闭

#pragma mark

#pragma mark tableView delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 1;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPat

{

    static NSString * identifier =
@"default";

    UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier
:identifier];

    

    if (cell == nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];

        cell.selectionStyle=UITableViewCellSelectionStyleNone;

    }

    

    cell.textLabel.font = [UIFont
fontWithName:@"arial"
size
:15];

    cell.textLabel.numberOfLines
= 0;

    cell.textLabel.text =
self.description;

    

    return cell;

}

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

{

    /*根据数据(self.description)得到数据大小.
然后用设置字的大小(sizeWithFont:.

     *再判断是iPhone还是iPad.设置宽度(isPhone?300:950.
然后设置高度(CGFLOAT_MAX.

     */

    CGSize
requiredSize = [self.descriptionsizeWithFont:[UIFontsystemFontOfSize:15] constrainedToSize:CGSizeMake(isPhone?300:950,
CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];

    

    return requiredSize.height+20;

}

抱歉!评论已关闭.