没有显示UITableViewCell中的UILabel子视图. (iPhone开发人员) [英] UILabel subview in UITableViewCell isn't showing up. (iPhone Dev)

查看:60
本文介绍了没有显示UITableViewCell中的UILabel子视图. (iPhone开发人员)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在具有UILabel作为子视图的表视图中有一个单元格,当我设置标签的文本时,它永远不会显示在该单元格上.我在iPhone开发方面有些菜鸟,不知道自己做错了什么...

I have a cell in a table view that has a UILabel as a subview, when I set the text of the label, it is never shown on the cell. I'm somewhat a noob in iPhone development and am not sure what I'm doing wrong...

我正在表视图的cellForRowAtIndexPath委托中创建并返回以下单元格:



cell = [[[ActivityCell alloc] initWithFrame:
    CGRectZero reuseIdentifier:ColumnedCell] autorelease];

CGRect frame = CGRectMake(180.0, 11.0, 130.0, 22);

UILabel *valueField = [[UILabel alloc] initWithFrame:frame];
[valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
valueField.tag = 111;
valueField.textAlignment = UITextAlignmentRight;
valueField.textColor = [UIColor colorFromHex:@"326799"];
valueField.highlightedTextColor = [UIColor whiteColor];
valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
valueField.adjustsFontSizeToFitWidth = YES;

[cell.contentView addSubview:valueField];
[valueField release];

cell.selectionStyle = UITableViewCellSelectionStyleNone;


然后,我想稍后通过执行以下操作来设置UILabel(子视图)(我目前在viewDidAppear中有此代码):



ActivityCell *cell = (ActivityCell*)[formDetailsTableView cellForRowAtIndexPath:
    [NSIndexPath indexPathForRow:0 inSection:1]];

UITextField *valueField = (UITextField *)[cell viewWithTag:111];
valueField.text = @"foo";

但UILabel中永远不会出现"foo"一词.我相信在使用< = 2.2 SDK进行编译时会显示出来,但是我想在3.0+版本中使用它

But the word "foo" never shows up in the UILabel. I believe it shows up when I compile with <= 2.2 SDK, but I want to get this working with 3.0+

为什么没有显示文本的任何想法?

Any ideas of why the text isn't showing up?

跟进:

请对此问题进行后续处理:

Please take a look at this question as a follow up to this: UITableViewCell's textLabel is overlapping a subview label, how can I fix it? (iPhone Dev)

推荐答案

首先,您要创建一个UILabel,将其标记为111,然后将其作为子视图添加到单元格的contentView中.稍后,您尝试检索该UILabel,但将其强制转换为UITextField.从根本上讲,您在这里所做的事情是错误的.

Well first off, you're creating a UILabel, tagging it as 111, and then adding it as a subview to the cell's contentView. Later on, you are trying to retrieve that UILabel but you're casting it to a UITextField. Fundamentally something you're doing here is wrong.

这是我建议做的事情:

  1. 请参阅Apple的
  1. Take a look at Apple's guide for using Interface Builder to create cells. It's going to lead to a lot less code and you'll be able to easily see what's going on, and won't have to add the Label/TextField programatically.
  2. Instead of trying to access cells directly in viewDidAppear and changing their subviews, you should be doing all of that display logic inside of cellForRowAtIndexPath. In another method (for example, one called when the user touches a button), you'll modify some data structure and then send the tableView the reloadData message. At that time, cellForRowAtIndexPath is called again and the values set in your data structure determines how the cell is created.

这篇关于没有显示UITableViewCell中的UILabel子视图. (iPhone开发人员)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆