读书人

TableCell自适应报表高度

发布时间: 2012-08-03 00:12:14 作者: rapoo

TableCell自适应表格高度

  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  2. // 列
  3. CGFloat contentWidth = self.tableView.frame.size.width;
  4. // 用何字行示
  5. UIFont *font = [UIFont systemFontOfSize:13];
  6. // 行要示的容
  7. NSString *content = [data objectAtIndex:indexPath.row];
  8. // 算出示完容需要的最小尺寸
  9. CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
  10. // 返回需要的高度
  11. return size.height;
  12. }
  13. // Customize the appearance of table view cells.
  14. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  15. static NSString *CellIdentifier = @"Cell";
  16. // 列
  17. CGFloat contentWidth = self.tableView.frame.size.width;
  18. // 用何字行示
  19. UIFont *font = [UIFont systemFontOfSize:13];
  20. // 行要示的容
  21. NSString *content = [data objectAtIndex:indexPath.row];
  22. // 算出示完容需要的最小尺寸
  23. CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
  24. // 建示行
  25. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  26. if (cell == nil) {
  27. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  28. }
  29. CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];
  30. // 置示榘形大小
  31. rect.size = size;
  32. // 重置列文本域
  33. cell.textLabel.frame = rect;
  34. cell.textLabel.text = content;
  35. // 置自行(重要)
  36. cell.textLabel.numberOfLines = 0;
  37. // 置示字(一定要和之前算使用字一至)
  38. cell.textLabel.font = font;
  39. return cell;
  40. }

读书人网 >移动开发

热点推荐