gpt4 book ai didi

iOS : Customizing TableViewCell - Initializing Custom Cell

转载 作者:可可西里 更新时间:2023-11-01 04:09:12 27 4
gpt4 key购买 nike

在我的 TableView 中,我有一个 NSMutableArray *currList 的数据源 - 它包含对象 Agent 的对象。我创建了自定义的 TableCell 并正确设置了所有内容。我在显示数据时发现问题:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"ChatListCell";

// Custom TableViewCell
ChartListCell *cell = (ChartListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
// I believe here I am going wrong
cell = [[ChartListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSLog(@"Cell = %@", cell); // Shows null
}
/*
With UITableViewCell, things are working perfectly fine
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
*/
Agent *agent = (Agent *)[currChatList objectAtIndex:indexPath.row];
NSLog(@"Agent name - %@", agent.name); // Prints proper data
cell.nameLabel.text = agent.name;
cell.thumbImageView.image = [UIImage imageNamed:agent.photo];
cell.timeLabel.text = agent.chatTime;

return cell;
}

如您在上面的代码注释中所见,如果我注释自定义 ChartListCell 并使用 UITableViewCell,它会正常工作。但是对于 ChartListCell,什么也没有出现,在日志中我得到“Cell = null”并且代理名称显示正确。单元格不应为空。为什么它为空,任何人都可以帮我解决这个问题。我在哪里做错了?

非常感谢任何帮助。

谢谢

最佳答案

导入你的自定义单元格文件并试试这个

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

static NSString *simpleTableIdentifier = @"ChartListCell";

ChartListCell *cell = (ChartListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChartListCell" owner:self options:nil];
cell = [nib objectAtIndex:0];

}

Agent *agent = (Agent *)[currChatList objectAtIndex:indexPath.row];
NSLog(@"Agent name - %@", agent.name); // Prints proper data
cell.nameLabel.text = agent.name;
cell.thumbImageView.image = [UIImage imageNamed:agent.photo];
cell.timeLabel.text = agent.chatTime;

return cell;
}

关于iOS : Customizing TableViewCell - Initializing Custom Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22104792/

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