纯代码TableView自适应高度(很老的使用方法)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.numberOfLines = 100;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.text = self.arr[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     
    NSString* text = self.arr[indexPath.row];
     
    CGSize constraint = CGSizeMake(375-40, 100);
     
     
    NSAttributedString* attributedText = [[NSAttributedString alloc]initWithString:text attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
     
    CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil];
     
    CGSize size = rect.size;
     
    CGFloat height = MAX(size.height, 20);
    NSLog(@"%f",height);
    return height+10;
     
     
}

posted @ 2015-10-26 09:13  韦韦韦  阅读(454)  评论(0编辑  收藏  举报