gpt4 book ai didi

ios - 无法在自定义表格 View 单元格上设置属性

转载 作者:行者123 更新时间:2023-11-29 10:48:29 26 4
gpt4 key购买 nike

我在自定义单元格上设置 View 元素时遇到问题。表格单元格出现在我的 tableView 中,但未设置属性,因此只出现空/空白单元格。

tableView不是tableView Controller ,只是viewController中的tableView。

我有以下文件:

自定义单元格.xib:
在这里,我使用 IB 通过使用带有标签和图像的对象库中的 TableView 单元来构建自定义单元。我将标识符设置为 orderListCell。在这个屏幕上,我按住 ctrl 并拖动以在 customCell.h

中创建导出

CustomCell.h
在这里,我将所有 IBOutlets 视为上述文件中的属性

CustomCell.m
在这里我保持原样

OrderListViewController.h
我在这里导入 customCell.h 并使用协议(protocol) UITableViewDelegate、UITableViewDataSource

OrderListViewController.m
在这里,我将 tableView delegate 和 tableView dataSource 设置为 self。我还从 Storyboard 中为我的 tableView 创建了一个 IBOutlet。

我使用以下代码来尝试显示我的单元格:

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

static NSString *cellIdentifier = @"orderListCell";

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) {
cell = [[CustomCell alloc] init];
}

cell.myLabel.text = @"aaaaaaaa"; //[[self.orders objectAtIndex:indexPath.row] objectForKey: @"tableNo"];



return cell;
}

我已经稍微简化了我的代码,以证明即使将标签设置为一个简单的字符串 (@"aaaaaaaa") 也不起作用。当我查看调试器中的对象时,单元格确实具有 IBOutlets 的所有属性,并且单元格确实出现在我的 tableView 中,只是 label.text = xxx 似乎不起作用。 p>

我看过以下帖子,但要么没有正确理解,要么对我不起作用:

ios 7 customizing UITableViewCell's content view
Can't set properties in Custom UITableViewCell
Set label for a custom cell

如有任何帮助或建议,我们将不胜感激

最佳答案

cell = [[CustomCell alloc] init];不会从 XIB 创建您的单元格,因此不会创建任何 XIB 内容。

使用 registerNib:forCellReuseIdentifier:向 TableView 注册 XIB,以便它为您创建单元格实例(您不需要自己在 cellForRowAtIndexPath 中创建实例)。

如果您不想这样做,您可以使用 nibWithNibName:bundle: 显式加载您的 NIB和 instantiateWithOwner:options: ,然后从返回的 View 列表中获取单元实例(它应该只包含 1 个项目)。

关于ios - 无法在自定义表格 View 单元格上设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21459084/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com