ios - 表中单元格的自定义组件

标签 ios objective-c uitableview

如何将不同的组件添加到表格 View 中的不同单元格。请注意,这是一个静态表格,我只有 4 个单元格。

第一个单元格将有一个 UIImageView。

其他 3 个单元格将只有标签或文本字段。

我如何添加这些组件。

注意:这是一个基于 Storyboard的应用程序,我已经添加了

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell;
    if(indexPath.row == 0)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell0"];
        cell.profImageView.image=[UIImage imageNamed:@"m.jpg"];
        return cell;
    }
    else if(indexPath.row == 1)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.lbl.text=@"Hey";
        return cell;
    }
    else if(indexPath.row == 2)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        cell.lbl.text=@"Hey 2";
        return cell;
    }
    return cell;
}

最佳答案

您的数据源中有 4 行,但您只为 3 行提供单元格对象。最后一个 return 语句将针对 indexPath.row==3 执行,而您尚未对其进行初始化。只需为 indexPath.row==3 初始化它就可以了。

关于ios - 表中单元格的自定义组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29070480/

相关文章:

ios - 复选框图标未正确显示

ios - HKMetadataKeyWasUserEntered 是否损坏?当健康应用程序中有数据时,我总是得到零

iOS布局列到行

ios - 如何以只读方式设置 UICollectionViewCell 的属性

ios - 显示键盘时 UITableview 不会向上滚动

iphone - 点击tabBar时刷新tableView

ios - UITableviewcell内容更新反射(reflect)

objective-c - 将日期时间从 NSString 转换为 NSDate

ios - 使用我的应用程序处理 http url(如果已安装)

uitableview - 使用自定义 iOS UITableView 单元格的问题/"Multiple segues with identifier"错误