iOS xcode,将 UILabel 子视图添加到自定义表格单元格,标签文本不会在屏幕上更新

发布于 2024-12-04 07:18:08 字数 1942 浏览 0 评论 0原文

我创建了一个自定义 UITableViewCell 类,并使用layoutSubviews 方法添加自定义标签。像这样:

- (void)layoutSubviews
{
    [super layoutSubviews];

    if (statusLabel == nil)
    {
        statusLabel = [[UILabel alloc]initWithFrame:CGRectMake(430.0, 10.0, 100.0, 20.0)];
        [statusLabel setTextAlignment:UITextAlignmentRight];
        [statusLabel setText:@"Status, set in code"];
        statusLabel.tag = 1;

        [self.contentView addSubview:statusLabel];
    }        
}

如您所见,我已将标签的初始文本设置为“状态,在代码中设置”。

在表视图控制器中,我在 cellForRowAtIndexPath 方法中设置此自定义标签的文本,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    int index = [indexPath row];

    NSString *introducerString =[introducers objectAtIndex:index];
        NSArray *parts = [introducerString componentsSeparatedByString:@","];

    static NSString *MyIdentifier = @"Requester";
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) 
    {
        cell = [[[DanceCardCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:MyIdentifier] autorelease];
    }

    UIImage *image = [UIImage imageNamed:@"1.jpg"];
    [cell.imageView setImage:image];
    cell.textLabel.text = [parts objectAtIndex:1];
    cell.detailTextLabel.text = @"Some text";

    UILabel *statusLabel = (UILabel *)[cell viewWithTag:1];
    statusLabel.text = @"Did it!";

    return cell;
}

我使用一个表视图来显示两个列表,具体取决于按下两个按钮中的哪一个。当按下按钮时,相应的表视图控制器将附加到表视图,并调用 reloadData 方法来触发新数据的显示。新数据确实显示,但自定义标签文本应显示为“Did it!”读取“状态,在代码中设置...”,直到我再次切换列表两次。

如何让自定义标签的新文本立即更新?我已经检查了官方文档,但找不到任何关于更新自定义内容后刷新单元格显示的参考。

以下屏幕截图演示了发生的情况:http://www.dsbsystems.co。英国/images/xcode1.png

I've created a custom UITableViewCell class, and used the layoutSubviews method to add a custom label. Like this:

- (void)layoutSubviews
{
    [super layoutSubviews];

    if (statusLabel == nil)
    {
        statusLabel = [[UILabel alloc]initWithFrame:CGRectMake(430.0, 10.0, 100.0, 20.0)];
        [statusLabel setTextAlignment:UITextAlignmentRight];
        [statusLabel setText:@"Status, set in code"];
        statusLabel.tag = 1;

        [self.contentView addSubview:statusLabel];
    }        
}

As you can see, I have set the initial text of the label to "Status, set in code".

In the table view controller I set the text for this custom label in the cellForRowAtIndexPath method, like so:

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    int index = [indexPath row];

    NSString *introducerString =[introducers objectAtIndex:index];
        NSArray *parts = [introducerString componentsSeparatedByString:@","];

    static NSString *MyIdentifier = @"Requester";
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) 
    {
        cell = [[[DanceCardCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:MyIdentifier] autorelease];
    }

    UIImage *image = [UIImage imageNamed:@"1.jpg"];
    [cell.imageView setImage:image];
    cell.textLabel.text = [parts objectAtIndex:1];
    cell.detailTextLabel.text = @"Some text";

    UILabel *statusLabel = (UILabel *)[cell viewWithTag:1];
    statusLabel.text = @"Did it!";

    return cell;
}

I'm using one table view to display two lists, depending on which of two buttons is pressed. When a button is pressed the appropriate table view controller is attached to the table view, and the reloadData method is called to trigger display of the new data. The new data does display, but the custom label text, which should read "Did it!" reads "Status, set in code ...", until I switch lists again twice.

How can I get the new text for the custom label to update straight away? I have checked the official documentation and cannot find any reference to refreshing a cell's display after updating its custom content.

Here is a screen shot to demonstrate what happens: http://www.dsbsystems.co.uk/images/xcode1.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

临走之时 2024-12-11 07:18:08

您正在初始化单元格并立即尝试查找其中包含标签 1 的 statusLabel。 layoutSubviews 还没有机会被调用,因此标签还没有被创建和添加。 (我建议重写表视图单元格上指定的初始化方法并在其中创建标签。)

因此,当您尝试拉出 statusLabel 时,它会变为 nil 因为没有这样的视图,并且消息传递(调用方法) nil 根本不执行任何操作(实际上,它返回 nil)。如果您习惯于因空引用异常而导致崩溃,那么您需要注意这一点。

当再次请求该单元时,不需要新单元,因为它可以从重用队列中获得,并且可以正确找到标签。

You're initializing the cell and immediately attempting to find the statusLabel with tag 1 inside it. layoutSubviews hasn't had the opportunity to be called yet, and so the label hasn't been created and added. (I suggest overriding the designated initializer method on your table view cell and creating the label there.)

Because of this, when you try to pull out statusLabel, it becomes nil because there's no such view, and messaging (calling a method on) nil simply does nothing (actually, it returns nil). You will need to watch out for this going forward if you're used to things blowing up with e.g. null reference exceptions.

When the cell is requested again, a new cell isn't needed because it's available from the reuse queue, and the label will be found correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文