ios - 动态添加对象到 UITableView 单元格

标签 ios uitableview

enter image description here ![enter image description here][2]我有一个表格 View ,我在其中使用我的自定义单元格,其中包含用户的个人资料图片、状态标签、时间戳标签、用户名标签、没有评论标签和评论按钮。 当没有评论,即“4条评论”被点击时,我必须在原始帖子下方的相应单元格中显示评论以及用户名、用户个人资料图片等。 如何在应用程序运行时将这些对象动态添加到单元格中?或者还有其他解决方案请告诉。 2 个帖子的表格显示在图像的右侧。应显示点击图像左侧的评论。 这是表格单元格的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



    static NSString *CellIdentifier = @"MyCell";
    TableCell *cell = (TableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self   options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (TableCell *)currentObject;
                break;
            }
        }
    }

    // Configure the cell.
    User *user = [postTableData objectAtIndex:indexPath.row];
    NSLog(@"pic:%@",user.profilePictureURL);
    cell.profilePicture.image = [UIImage imageNamed:@"avatar.png"];
    cell.profilePicture.layer.cornerRadius = cell.profilePicture.frame.size.height /2;
    cell.profilePicture.layer.masksToBounds = YES;
    cell.profilePicture.layer.borderWidth = 0;

    cell.userName.text = user.name;
    cell.status.text = user.status;
    cell.dateForStatus.text = user.datePosted;
    [cell.countComments setTitle:user.countOfComments forState:UIControlStateNormal];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.contentView.layer setBorderWidth:6.0f];
    [cell.contentView.layer setBorderColor:[[UIColor alloc] initWithRed:233/255.0 green:236/255.0 blue:233/255.0 alpha:1.0].CGColor];
    cell.commentButton.tag=indexPath.row;
    [cell.commentButton addTarget:self action:@selector(commentButtonPressAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    ![enter image description here][3]}

最佳答案

我猜你正在尝试扩展单元格。

您可以调用这几行来更新单元格,包括高度。

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];

因此只需设置一个标志以在 cellForRowAtIndexPath 中显示注释。

//AFTER_YOUR_SETTING
if(showComment){
    [cell loadCommentMethod];
}

还有,在 heightForRowAtIndexPath

if(showComment){
    return [CellClass publicMethodForCalHeight:dataAtIndexPath];
} else {
    return kDefaultCellHeight;
}

关于ios - 动态添加对象到 UITableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22346079/

相关文章:

iphone - UISlider 和连续属性

objective-c - UITableView 重新排序隐藏背景

iOS 自定义 UITableViewCell 隐藏顶角溢出

ios - 异步加载后更新 UITableView 中的数据

iphone - UITableView 在顶部添加行

ios - 如何在IOS中获取上次通话的详细信息

ios - Swift:创建一个调用另一个类 1 中的方法的按钮

ios - 获取图像时 CFNetwork 内存峰值

ios - 自定义 MKMarkerAnnotationView 第一次在 UITableViewCell 中无法正确显示

ios - 当我的 UITableViewCell 不再可见时,如何取消对象中的 NSOperation?