我有一个 tableView
,每个 cell
都包含 Twitter 用户的用户名和推文:http://postimg.org/image/5p8y9wh47/
但是正如您从图片中看到的那样,它并没有显示用户的整个推文。我如何让它显示用户的整个推文并调整整个表格 View 单元格的大小?
我知道我必须使用 heightForRowAtIndexPath
但我什至不知道如何开始......
cell.textLabel.text = [NSString stringWithFormat:@"%@ @%@", [tweet[@"user"] objectForKey:@"name"], [tweet[@"user"] objectForKey:@"screen_name"]];
cell.detailTextLabel = [NSString stringWithFormat:@"%@", tweet[@"text"]];
我如何获得 textLabel 和 detailTextLabel 的高度(以及整个推文)并相应地更改单元格大小?
更新
我一直在尝试:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//get tweet data
NSDictionary *tweet = _userPosts[indexPath.row];
//create a string for the header - name (@username)
NSString *header = [NSString stringWithFormat:@"%@ @%@", [tweet[@"user"] objectForKey:@"name"], [tweet[@"user"] objectForKey:@"screen_name"]];
NSString *tweetData = [NSString stringWithFormat:@"%@", tweet[@"text"]];
CGSize constraintSize = CGSizeMake(320.0f, MAXFLOAT);
CGSize labelSize = [header sizeWithAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:14]} constrainedToSize:CGSizeMake(_feedTableView.bounds.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
}
最佳答案
在 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
中,您的做法是正确的
查看 Apple 的 NSString UIKit Additions Reference 以了解如何获取字符串的大小:
您需要使用 – boundingRectWithSize:options:attributes:context:
来获取您的用户名和推文字符串的高度。
关于iOS - UITableViewCell 更改高度取决于数据单元格保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20850156/