iOS:自定义 TableViewCell - 初始化自定义单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22104792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS : Customizing TableViewCell - Initializing Custom Cell
提问by Tvd
In my TableView, I have a dataSource of NSMutableArray *currList - it contains objects of object Agent. I created customized TableCell and set up everything properly. I am finding problem while displaying data :
在我的 TableView 中,我有一个 NSMutableArray *currList 的数据源 - 它包含对象代理的对象。我创建了自定义的 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;
}
As you can see in above code comments, If I comment the custom ChartListCell and use UITableViewCell, it works properly. But with ChartListCell, nothing comes up and in logs I get "Cell = null" & Agent name is showing properly. Cell shouldn't be null. Why is it null, can anyone please help me out with this. Where am I doing mistake ?
正如您在上面的代码注释中看到的,如果我注释自定义 ChartListCell 并使用 UITableViewCell,它可以正常工作。但是对于 ChartListCell,什么也没有出现,并且在日志中我得到“Cell = null”并且代理名称显示正确。单元格不应为空。为什么它是空的,任何人都可以帮我解决这个问题。我在哪里做错了?
Any help is highly appreciated.
任何帮助都受到高度赞赏。
Thanks
谢谢
回答by Mohit
import your custom cell file and try this
导入您的自定义单元格文件并尝试此操作
- (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;
}
回答by Nitesh Borad
Basically There are four possible approaches for making custom UITableViewCells: If you are interested to learn all these four, it is worth to go through this link: http://www.apeth.com/iOSBook/ch21.html#_custom_cellswhich describes all these approaches beautifully.
基本上有四种可能的方法来制作自定义 UITableViewCells:如果你有兴趣学习所有这四种,值得通过这个链接:http: //www.apeth.com/iOSBook/ch21.html#_custom_cells描述所有这些方法很漂亮。
Just go through it for sake of your knowledge.
只是为了你的知识而通过它。