views:

408

answers:

4

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...

I am creating and returning the following cell in my table view's cellForRowAtIndexPath delegate:


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;


I then want to set the UILabel (subview) later by doing this (I currently have this code in viewDidAppear):


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

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

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?

FOLLOW UP:

Please take a look at this question as a follow up to this: http://stackoverflow.com/questions/1981260/uitableviewcells-textlabel-is-overlapping-a-subview-label-how-can-i-fix-it-i

A: 

It looks like you have a small bug:

change

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

to

UITextField *valueField = (UITextField *)[cell.contentView viewWithTag:111];
marcc
No luck, the valueField.text looks like it's set (via NSLog) but It doesn't show up in the view.
mmattax
A: 

My guess is that the cell coming back is nil and thus not updating your text. I would start buy making sure you are getting a cell back from this line:

ActivityCell *cell = (ActivityCell*)[formDetailsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
Tony Eichelberger
The cell is not null...
mmattax
+2  A: 

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.

Here's what I recommend doing:

  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.
bpapa
A: 
CiNN
The frame a table-view cell is initialized with is typically irrelevant, as the UITableView manages that along with a number of other aspects of the cell. This is probably why the designated initializer for `UITableViewCell`—`-initWithStyle:reuseIdentifier:`—now eliminates the "frame" parameter entirely.
Noah Witherspoon