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

UITableView的cell动态高度的方法

2013-05-28 18:12 441 查看
常规办法:

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

{

  if(0 == [indexPath row])

  {

    return 10;

  }

  

if(1 == [indexPath row])

  {

    return 30;

  }

}


然后:

- (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if( 0 == [indexPath row])
{
// xxxx cell.contoneview = [[UIView allco] initwhithframe:CGRectMake(0, 0, 80, 10)];
}
if( 1 == [indexPath row])
{
//ooo cell.contoneview = [[UIView allco] initwhithframe:CGRectMake(0, 0, 80, 30)];
}
}


比较好的做法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UILabel *label = (UILabel *)[cell viewWithTag:1];

CGRect cellFrame = [cell frame];
cellFrame.origin = CGPointMake(0, 0);

CGRect rect = CGRectInset(cellFrame, 100, 40);
label.frame = rect;

if (label.frame.size.height > 30)
{
cellFrame.size.height = 150 + label.frame.size.height -30;
}
else
{
cellFrame.size.height = 150;
}
[cell setFrame:cellFrame];

}

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: