问题描述
在此屏幕快照中,您可以看到我已经在UIViewController中添加了UITableView,然后通过在其中添加一些标签来自定义了UITableViewCell.但是问题是当我运行应用程序时.所有单元格都是空的.根本没有标签.
In this screen shot you can see that I have added UITableView in UIViewController then customized the UITableViewCell by adding some labels in it. But the issue is when I run the application. All of the cells are empty. There are no labels at all.
我没有发现可能是什么问题.我已经在网上搜索并阅读了教程,但无法解决.
I am not getting what can be the issue. I have searched the web and read tutorials but couldn't resolve it.
推荐答案
我不费吹灰之力就自己解决了此问题.
实际上,当您创建自定义单元时,您必须执行以下操作:
I resolved this issue by myself, just after little effort.
Actually when you create a custom cell you have to do these things:
- 在情节提要单元上设置标签,图像等.
- 创建一个自定义单元格类(从UITableViewCell继承)(CustomCell.h& .m),CustomCell.h具有所有属性,如标签,图像等的iboutlet,并在实现中将它们全部合成.
- 创建此自定义类后,返回情节提要,选择自定义单元格,将其类更改为CustomCell并为其赋予一些标识符,例如"MyCustomCell",然后右键单击自定义单元格,然后将IBOutlet与标签等连接.
-
现在在实现UITableView的类中导入CustomCell类,并使用在CustomCell类中定义的属性.
- Setup the labels, images etc on storyboard cell.
- Create a custom cell class (inheriting from UITableViewCell)(CustomCell.h & .m), CustomCell.h having all of the properties as iboutlet for labels, images etc. and synthesize them all in implementation.
- After creating this custom class go back to storyboard, select the custom cell, change its class to the CustomCell and give it some identifier like "MyCustomCell", then right click on the custom cell and connect the IBOutlets with labels etc.
Now import CustomCell class in the class where you are implementing the UITableView and use the properties you defined in CustomCell class.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyCustomCell";
CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the provided setImageWithURL: method to load the web image
// Ensure you use a placeholder image otherwise cells will be initialized with no image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder"]];
cell.myCustomLabel.text = @"My Text";
return cell;
}
我已全部完成,我的问题已解决,请不要忘记与代表联系&数据源和表.
I did this all and my issue was resolved and please don't forget to connect the delegates & datasource and table.
希望这对其他人有帮助.
Hope this will help others.
这篇关于自定义UITableViewCell不在情节提要中显示标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!