简体   繁体   English

UITableView的空白原型单元格

[英]Empty prototype cell for a UITableView

I have a new project where I had to drag a prototype cell to a existing table view. 我有一个新项目,必须原型单元拖到现有的表格视图中。 I then 然后我

  • added some labels to the prototype cell with appropriate tags 使用适当的标签向原型单元添加了一些标签
  • set a identifier for the cell 设置单元格的标识符

and then in my tableview delegate when I get the delegate callback : 然后在我的tableview委托中,当我得到委托回调时:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:11];

I see that nameLabel is nil. 我看到nameLabel为零。 I have double checked and tripled checked the tag and reusable identifier with no luck. 我已经仔细检查过标签,并且三重检查了标签和可重复使用的标识符,但是没有运气。 In the storyboard, I see that the tableview has the pro type cell as its cell with the contentView showing my labels. 在情节提要中,我看到tableview具有pro type单元格作为其单元格,并且contentView显示了我的标签。 What am I missing? 我想念什么?

Do you ever create a new cell in the if (cell == nil) branch? 您是否曾经在if (cell == nil)分支中创建一个新单元if (cell == nil) If yes, you are creating a regular UITAbleViewCell there. 如果是,则在此处创建常规的UITAbleViewCell。 Do not expect any custom lable there, because you don't load it from any nib file nor from the storyboard. 不要期望在那里有任何自定义标签,因为您不会从任何nib文件或情节提要中加载它。

Of what type is the cell object? 单元格对象是什么类型的? NSLog it or have a look in the debugger which type is actually created. NSLog或在调试器中查看实际创建的类型。

You are allocating UITableViewCell which don't contain your custom label. 您正在分配不包含自定义标签的UITableViewCell。 If you have created a View using Storyboard then you should allocate that view using following method. 如果使用Storyboard创建了一个View,则应使用以下方法分配该View。

You can create a Utility method to get Class instance from NibName 您可以创建一个Utility方法来从NibName获取Class实例。

+ (id)loadNibNamed:(NSString *)nibName ofClass:(Class)objClass 
{
    if (nibName && objClass) 
    {
        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName 
                                                     owner:nil 
                                                   options:nil];            
        for (id currentObject in objects )
        {
            if ([currentObject isKindOfClass:objClass])
                return currentObject;
        }
    }
    return nil;
}

Use this in your code like 在您的代码中像这样使用

CustomViewCell *cell = (CustomViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomViewCell"];
if (cell == nil)
{
    cell = [Utility loadNibNamed:@"CustomViewCell" ofClass:[CustomViewCell class]];
}

cell.yourLabel.text = @"Dummy Text";

Hopefully this will help you. 希望这会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM