UITableView高度自适应

来源:互联网 发布:软件售卖平台 编辑:程序博客网 时间:2024/05/01 04:53
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {// 列宽CGFloat contentWidth = self.tableView.frame.size.width;// 用何种字体进行显示UIFont *font = [UIFont systemFontOfSize:13];NSString *content = [data objectAtIndex:indexPath.row];CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];return size.height; }// Customize the appearance of table view cells.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {        static NSString *CellIdentifier = @"Cell";    // 列宽CGFloat contentWidth = self.tableView.frame.size.width;UIFont *font = [UIFont systemFontOfSize:13];NSString *content = [data objectAtIndex:indexPath.row];// 计算出显示完内容需要的最小尺寸CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];// 構建顯示行    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil) {        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];    }CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];rect.size = size;cell.textLabel.frame = rect;    cell.textLabel.text = content;cell.textLabel.numberOfLines = 0;cell.textLabel.font = font;    return cell;}
0 0
原创粉丝点击