iOS xcode,将 UILabel 子视图添加到自定义表格单元格,标签文本不会在屏幕上更新
我创建了一个自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在初始化单元格并立即尝试查找其中包含标签 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 becomesnil
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.