简体   繁体   English

自定义TableViewCell不显示标签

[英]Custom TableViewCell not displaying label

I'm struggling with a problem I encountered while trying to create a custom UITableViewCell. 我正在尝试创建自定义UITableViewCell时遇到问题。

I subclassed UITableViewCell in SGTableViewCell and added it in the prototype cell in the storyboard. 我将SGTableViewCell中的UITableViewCell子类化,并将其添加到情节提要的原型单元中。

原型单元的情节提要图板视图

As you can see the label is connected 如您所见,标签已连接 在此处输入图片说明

and the cell identifier is set correctly 并且单元格标识符设置正确 在此处输入图片说明

Then I linked the label to the SGTableViewCell.h like this 然后我像这样将标签链接到SGTableViewCell.h

@property (strong, nonatomic) IBOutlet UILabel *nameLabel;

and in the .m file I have this code 在.m文件中,我有此代码

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [self addGestureRecognizer:recognizer];

        self.nameLabel = [[UILabel alloc] init];
        _checkView = [[UIView alloc] initWithFrame:CGRectNull];
        _checkView.backgroundColor = kGreen;
        _checkView.alpha = 0.0;
        [self addSubview:_checkView];
        self.nameLabel.text = @"Hello";
        }
    return self;
 }

But when I use this cell in my tableview using this code 但是当我使用此代码在表格视图中使用此单元格时

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

    Episode *episode = [self.selectedSeason.episodeList objectAtIndex:indexPath.row];

    SGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Episode"];

    UIView *selectionColor = [[UIView alloc] init];
    selectionColor.backgroundColor = kSelectionGrey;

    cell.selectedBackgroundView = selectionColor;
    cell.backgroundColor = kBackgroundGrey;

    cell.nameLabel.text = episode.name;
    NSLog(@"%@", cell.nameLabel.text);
    cell.nameLabel.textColor = [UIColor redColor];

    return cell;
}

I get no text at all. 我什么也没收到。

I tried logging the text from each label in each cell and it gives me the right text. 我尝试记录每个单元格中每个标签的文本,它为我提供了正确的文本。 I tried setting programmatically a different disclosure indicator for the custom cell and it did change so everything is allocated and working but label is not displaying. 我尝试以编程方式为自定义单元格设置了一个不同的公开指示器,但它确实发生了更改,因此所有内容均已分配并可以工作,但标签未显示。 I honestly have no idea of what's the problem. 老实说,我不知道出了什么问题。 Did I miss something? 我错过了什么? Thank you 谢谢

PARTIALLY SOLVED: 部分解决:

OK i tried doing the same thing on an empty project and everything worked flawlessly so I checked again my project and found this line 好吧,我尝试在一个空项目上做同样的事情,并且一切正常,所以我再次检查了项目并发现了这一行

[self.tableView registerClass:[SGTableViewCell class] forCellReuseIdentifier:@"Episode"];

Seeing it was not necessary for the empty project i commented this line and everything started working. 看到没有必要进行空项目时,我评论了这一行,一切开始正常进行。

The only problem i have now is that if i don't use this line i can't use the custom cell as was intended. 我现在唯一的问题是,如果我不使用此行,则无法按预期使用自定义单元格。 In fact my custom cell is swipable using a pan gesture recognizer but without registering my custom class to the tableview seems like the swipe doesn't work. 实际上,我的自定义单元格可以使用平移手势识别器进行滑动,但是如果不将我的自定义类注册到表视图中,则滑动似乎不起作用。

Sorry for the trouble, seems like i messed up again :/ 对不起,麻烦了,好像我又搞砸了:/

You shouldn't alloc init a label that you created in the storyboard, it is already allocated automatically. 您不应该分配在情节alloc init创建的标签,它已经被自动分配。 When you do self.nameLabel = [[UILabel alloc] init]; 当你做self.nameLabel = [[UILabel alloc] init]; , you reset the self.nameLabel property to point to a new empty memory location and not to the label created in the storyboard, hence you can change its text property and see the result in NSLog but not in the storyboard because it doesn't refer to that label in the storyboard. ,您将self.nameLabel属性重置为指向新的空内存位置,而不是指向self.nameLabel创建的标签,因此您可以更改其text属性并在NSLog中查看结果,但不能在情节提要中查看,因为它没有引用故事板上的那个标签。

Try removing all initialisation from the initWithStyle method (to make sure nothing is covering it such as that subview you create), and everything related to the label in the cellForRowAtIndexPath method (same reason), and try a simple assignment like self.nameLabel.text = @"Test text" in the cellForRowAtIndexPath method, it should work. 尝试从initWithStyle方法中删除所有初始化(以确保没有任何东西覆盖住您创建的子视图之类的内容),以及与cellForRowAtIndexPath方法中与标签相关的所有内容(相同的原因),然后尝试进行简单的赋值,例如self.nameLabel.text = @"Test text"cellForRowAtIndexPath方法中self.nameLabel.text = @"Test text" ,它应该可以工作。 Then add all your other initialisation. 然后添加所有其他初始化。

And yeah, don't forget to input your cell reuse identifier "Episode" in the storyboard. 是的,不要忘记在情节提要中输入单元重用标识符“ Episode”。

Make sure you: 确保你:

  1. Have linked the delegate and the datasource to the view the tablview is housed in. 已将委托和数据源链接到tablview所在的视图。

  2. Have that view implement UITableViewController and UITableViewDelegate (I'm pretty sure it is both of those). 让该视图实现UITableViewControllerUITableViewDelegate (我很确定两者都是)。

  3. Implement the necessary methods, which you seem to have done. 实现您似乎已经完成的必要方法。 You need the row size, section size, and the add cell methods 您需要row size, section size, and the add cell方法

  4. After updating the array linked to your tableview, call [ tableView reloadData ] 更新链接到tableview的数组后,调用[ tableView reloadData ]

Have a look at this link: Tutorial to create a simple tableview app 看一下此链接: 创建一个简单的tableview应用程序的教程

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

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