您的位置:首页 > 产品设计 > UI/UE

iOS - UITableViewCell自适应高度

2013-07-01 23:33 531 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息