我想实现的是这样的。它是一种 Facebook 或 twitter 功能,您有一个 TableView ,其中有一个线程,线程包含不同数量的文章。每行的文章数量各不相同。所以基本上就像我在 facebook 上发了一个帖子,人们回复了那个帖子,这些帖子被添加到那个特定的线程下(我只关心如何显示它,其他所有事情都被处理了)。这里是图片
我知道如何创建单元格等等,但我不知道如何动态设置它的大小。任何教程或任何建议来实现这一目标?
感谢任何帮助..
谢谢
最佳答案
也许这会对你有所帮助。我正在向您发送我的代码,我正在使用 Custom Cell ,它在第一个索引上有一个 Emial,在第二个索引上有 PhoneNumber,在第三个索引上有 Address(在你的例子中是 Article)。我正在动态更改 CellForRowAtIndexPath 中地址标签的高度,以及 heightForRowAtIndexPath 方法中的单元格高度。这是我的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = @"FeedCell";
FeedDetailCell *cell = (FeedDetailCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
cell = (FeedDetailCell*)[[[NSBundle mainBundle] loadNibNamed:@"FeedDetail" owner:nil options:nil] objectAtIndex:0];
}
[tableView setSeparatorColor:[UIColor grayColor]];
switch (indexPath.section) {
case 0:
if (indexPath.row == 0) {
cell.nameLabel.text = @"Telephone";
[cell.detailLabel setText:[_feedDictionary valueForKey:@"mobile"]];
}
else {
cell.nameLabel.text = @"Mobile";
[cell.detailLabel setText:[_feedDictionary valueForKey:@"iPhone"]];
}
break;
case 1:
cell.nameLabel.text = @"E-mail";
[cell.detailLabel setText:[_feedDictionary valueForKey:@"Email"]];
break;
case 2:
cell.nameLabel.text = @"address";
[cell.detailLabel setText:[_feedDictionary valueForKey:@"address"]];
CGSize size = [[_feedDictionary valueForKey:@"address"] sizeWithFont:[UIFont systemFontOfSize:14.0]
constrainedToSize:CGSizeMake(200.0, 400.0) lineBreakMode:UILineBreakModeWordWrap];
CGRect frm = cell.detailLabel.frame;
frm.size.height = size.height;
[cell.detailLabel setFrame:frm];
default:
break;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 2) {
NSString *address = [_feedDictionary valueForKey:@"address"];
CGSize recommendedSize = [address sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(320, INT_MAX)];
return 44 + recommendedSize.height;
}
else {
return 44;
}
}
关于objective-c - 如何在 ios 中为 Tableview 创建动态 TableCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12400253/