UITableViewCell中的UILabel子视图未显示。 (iPhone Dev)

时间:2009-12-28 19:41:35

标签: iphone objective-c

我在表格视图中有一个单元格,其中UILabel作为子视图,当我设置标签的文本时,它永远不会显示在单元格上。我在iPhone开发方面有点像菜鸟,我不确定我做错了什么......

我在表格视图的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";

但是“foo”这个词永远不会出现在UILabel中。我相信它会在我使用< = 2.2 SDK进行编译时显示出来,但我希望能够使用3.0 +

为什么文字没有出现的任何想法?

关注:

请关注此问题:UITableViewCell's textLabel is overlapping a subview label, how can I fix it? (iPhone Dev)

4 个答案:

答案 0 :(得分:2)

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

以下是我的建议:

  1. 查看Apple的using Interface Builder to create cells指南。它将导致更少的代码,您将能够轻松地看到正在发生的事情,并且不必以编程方式添加Label / TextField。
  2. 您应该在cellForRowAtIndexPath内部执行所有显示逻辑,而不是尝试直接在viewDidAppear中访问单元格并更改其子视图。在另一种方法中(例如,当用户触摸按钮时调用的方法),您将修改一些数据结构,然后向tableView发送reloadData消息。那时,再次调用cellForRowAtIndexPath,数据结构中设置的值决定了单元格的创建方式。

答案 1 :(得分:0)

看起来你有一个小bug:

更改

UITextField *valueField = (UITextField *)[cell viewWithTag:111];

UITextField *valueField = (UITextField *)[cell.contentView viewWithTag:111];

答案 2 :(得分:0)

我猜测回来的单元格为零,因此不会更新您的文本。我会开始购买,确保你从这一行得到一个单元格:

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

答案 3 :(得分:-1)

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


你的单元格有一个CGRectZero框架