objective-c - 如何在 ios 中为 Tableview 创建动态 TableCell

标签 objective-c ios uitableview

我想实现的是这样的。它是一种 Facebook 或 twitter 功能,您有一个 TableView ,其中有一个线程,线程包含不同数量的文章。每行的文章数量各不相同。所以基本上就像我在 facebook 上发了一个帖子,人们回复了那个帖子,这些帖子被添加到那个特定的线程下(我只关心如何显示它,其他所有事情都被处理了)。这里是图片enter image description here

我知道如何创建单元格等等,但我不知道如何动态设置它的大小。任何教程或任何建议来实现这一目标?

感谢任何帮助..

谢谢

最佳答案

也许这会对你有所帮助。我正在向您发送我的代码,我正在使用 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/

相关文章:

ios - 设置值 :ForKey: instead of set(whatever)?

iphone - 构建 10x10 UIButton 网格的最佳方法?

ios - 如何在 Facebook SDK 中创建自定义分享按钮

ios - 如何使用快速代码在按钮单击中的自定义 UITableViewCell 中显示 UIAlertController?

ios - iphone 应用程序崩溃 -[UITableView _selectRowAtIndexPath :animated:scrollPosition:notifyDelegate:]

objective-c - NSString 即使在保留后也被释放

ios - 从 plist 刷新数据

ios - KVO 观察 AVPlayer 导致 iOS 13 中的 App 崩溃

iphone - UITableViewCell 显示索引路径行乱序

ios - 如何制作自定义 TableView ,其中如果行是一个 TableView 也显示为一行表