UITableView的cell动态高度的方法
常规办法:
- (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; }