xcode 自定义 UITableViewCell 不显示故事板中的标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11339054/
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
Custom UITableViewCell does not show the labels in storyboard
提问by Omer Waqas Khan
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.
在此屏幕截图中,您可以看到我在 UIViewController 中添加了 UITableView,然后通过在其中添加一些标签来自定义 UITableViewCell。但问题是当我运行应用程序时。所有的单元格都是空的。根本没有标签。
I am not getting what can be the issue. I have searched the web and read tutorials but couldn't resolve it.
我不明白可能是什么问题。我已经在网上搜索并阅读了教程,但无法解决。
回答by Omer Waqas Khan
I resolved this issue by myself, just after little effort.
Actually when you create a custom cell you have to do these things:
我自己解决了这个问题,不费吹灰之力。
实际上,当您创建自定义单元格时,您必须执行以下操作:
- 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; }
- 在故事板单元格上设置标签、图像等。
- 创建一个自定义单元格类(从 UITableViewCell 继承)(CustomCell.h & .m),CustomCell.h 具有所有属性作为标签、图像等的 iboutlet,并在实现中合成它们。
- 创建此自定义类后,返回故事板,选择自定义单元格,将其类更改为 CustomCell 并为其提供一些标识符,例如“MyCustomCell”,然后右键单击自定义单元格并将 IBOutlets 与标签等连接起来。
现在在您实现 UITableView 的类中导入 CustomCell 类,并使用您在 CustomCell 类中定义的属性。
-(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.
希望这会帮助其他人。
回答by death7eater
It is little bit late but you can solve your problem by setting the background color of your view as Clear Color from your storyboard.
现在有点晚了,但您可以通过将视图的背景颜色设置为故事板中的清除颜色来解决您的问题。
回答by iremk
in the tableview's delegate method , cellforrowatindexpath has a uitableviewcell inside it , there should be an identifier in the initialization of this cell. possibly it is "cell" or "cellIdentifier" .
在tableview的delegate方法中,cellforrowatindexpath里面有一个uitableviewcell,这个cell的初始化应该有一个标识符。可能是 "cell" 或 "cellIdentifier" 。
you just need to select your cell from storyboard and enter this identifier string to the storyboard , where uitableviewcell's attribute inspector stays.
您只需要从 storyboard 中选择您的单元格并将此标识符字符串输入到 storyboard 中,uitableviewcell 的属性检查器就在那里。
hope this helps.. :)
希望这可以帮助.. :)
回答by Gaurav Patel
First Set cell identifier in storyboard @"Cell"
首先在故事板@“Cell”中设置单元格标识符
then set tag of label
然后设置标签标签
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
UILabel *lblDate = (UILabel*)[cell viewWithTag:101];
UILabel *lblDistance = (UILabel*)[cell viewWithTag:102];
UILabel *lbltype = (UILabel*)[cell viewWithTag:103];
lblDate.text = [temp objectAtIndex:indexPath.row] valueForKey:@"date"] stringByReplacingOccurrencesOfString:@"-" withString:@"/"]];
lblDistance.text = [NSString stringWithFormat:@"%@ KM",[[temp objectAtIndex:indexPath.row] valueForKey:@"distance"]];
NSString *type = [NSString stringWithFormat:@"%@",[[temp objectAtIndex:indexPath.row] valueForKey:@"distance"]];
if([type isEqualToString:@"0"])
{
lbltype.text = @"Personal";
}
else
{
lbltype.text = @"Bussiness";
}
// Configure
return cell;
}