【问题标题】:UITableViewCell with xib带有 xib 的 UITableViewCell
【发布时间】:2013-10-16 09:26:04
【问题描述】:

我正在为 iOS 开发一个使用 xib 文件的应用程序。

通常我使用 Storyboard 和 I don't know how to set up a UITableViewCell with xib files。当我用 UITableView 制作一个 xib 文件时,我看到一个有一些行的表,现在我需要编辑这一行来写入我存储在数组中的内容。

如何使用 xib 文件设计 UITableViewCell?

我需要做一个非常简单的表格:我会使用单元格的基本预设来显示标题。 我知道我必须将委托和 DataSource 连接到表视图的文件所有者,并且我将 UITableViewDelegate 和 UITableViewDataSource 放入文件所有者。

现在如何编辑单元格的内容? 我在网上找到了一些指南,告诉我用 UITableViewCell 创建一个 xib 文件,我做到了,但我不知道如何使用它

【问题讨论】:

    标签: ios objective-c cocoa-touch uitableview


    【解决方案1】:

    首先,您需要为继承自 UITableViewCell 的 customCell 创建一个类。 现在,在 customCell 中添加您想要的属性。在这个例子中,我添加了 cellImage 和 cellLabel。

    @property (nonatomic, strong) IBOutlet UILabel *cellLabel;
    @property (nonatomic, strong) IBOutlet UIImageView *cellImageView;
    

    之后,您需要将 UILabel 和 UIImageView 从 CustomCell 链接到 Nib。

    你需要添加:

    - (void)viewDidLoad 
    {
        ....
        [self.tableView registerNib:[UINib nibWithNibName:@"xibName" bundle:nil] forCellReuseIdentifier:CellIdentifier];
        .....
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"CustomCellReuse";
        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        // Configure the cell...
        [cell.cellImageView setImage:[UIImage imageNamed:@"whatever"]];
        [cell.cellLabel setText = @"whatever"];
        return cell;
    }
    

    【讨论】:

    • 当我执行以下操作时: NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];单元格 = [topLevelObjects objectAtIndex:0];它会加载自定义单元格,但不会以您的方式加载。什么是根本区别?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多