PDA

View Full Version : UITextView content disaapearing




cmezak
Jul 23, 2008, 12:48 PM
Hey all,

Haven't had any luck etting assistance in this forum yet, but I'll keep trying.

So I have a tableView that contains a cell that contains a UITextView as a subview. When I scroll so that this cell leaves the screen, and then scroll back, the textView's content is gone, aside from a few pixels on the left side of it. Here's my code to put the textView in the cell. See anything amiss?


textFrame = cell.contentView.frame;
textFrame.origin.x += 5;
textFrame.origin.y += 10;
textFrame.size.width = 250;
textFrame.size.height = 180;
UITextView *textView = [[UITextView alloc] initWithFrame:textFrame];
cellText = [[detailBird description] mutableCopy];
textView.text = cellText;
textView.editable = NO;
textView.userInteractionEnabled = NO;
[cell.contentView addSubview:textView];

Thanks!

Charlie



cmezak
Jul 28, 2008, 11:11 AM
Does anybody have any insight on this one? I still haven't been able to sort it out.

I've tried allocating and initializing the textView elsewhere in the code. Nothing seems to work. This is especially confusing to me because the same code seems to be working fine with instances of other UIViews like UILabel.

Whats the deal?

chuck

kainjow
Jul 28, 2008, 11:56 AM
So you're saying that exact code works if you use a different view?

You should initialize your view with CGRectZero, and override layoutSubviews (calling super first) and set its frame there based on contentView.bounds.

Edit: your code is leaking textView and cellText.

cmezak
Jul 28, 2008, 12:03 PM
Thanks for the reply.

The text in the cell shows up when I select the cell, but does not display when the cell is not selected . . . whats the deal?

I will post my entire code for cellForRowAtIndexPath here:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}

UIImage *image;
UILabel *labelView;
UILabel *dataView;
UITextView *textView;
NSMutableString *cellText = nil;
switch (indexPath.section) {
case 0:
image = [UIImage imageNamed:[[detailBird scientificName] stringByAppendingString:@".jpg"]];
cell.image = image;
//[image release];
break;
case 1:
switch (indexPath.row) {
case 0: //cell contains scientific name
//print label that says "scientific name: "
labelView = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 280, 20)];
labelView.text = @"Scientific Name:";
labelView.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[cell.contentView addSubview:labelView];
[labelView release];

//print object's scientific name
dataView = [[UILabel alloc] initWithFrame:CGRectMake(120, 5, 170, 20)];
dataView.text = [detailBird scientificName];
dataView.adjustsFontSizeToFitWidth = YES;
dataView.font = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
[cell.contentView addSubview:dataView];
[dataView release];
break;
case 1:
//cell contains Family
//print label that says "family:"
labelView = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 280, 20)];
labelView.text = @"Family:";
labelView.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[cell.contentView addSubview:labelView];
[labelView release];

//print object's family name
dataView = [[UILabel alloc] initWithFrame:CGRectMake(120, 5, 170, 20)];
dataView.text = [detailBird family];
dataView.adjustsFontSizeToFitWidth = YES;
dataView.font = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
[cell.contentView addSubview:dataView];
[dataView release];
break;
case 2:
//cell contains height
//print label that says "size:"
labelView = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 280, 20)];
labelView.text = @"Size:";
labelView.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[cell.contentView addSubview:labelView];
[labelView release];

//print object's size
dataView = [[UILabel alloc] initWithFrame:CGRectMake(120, 5, 120, 20)];
cellText = [[detailBird size] mutableCopy];
cellText = [[cellText stringByAppendingString:@"\""] mutableCopy];
dataView.text = cellText;
dataView.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
[cell.contentView addSubview:dataView];
[dataView release];
break;
default:
break;
}
break;
case 2:
//cell contains description
textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 200, 200)];
textView.text = [detailBird description];
NSLog(textView.text);
textView.editable = NO;
textView.userInteractionEnabled = NO;
[cell.contentView addSubview:textView];
//[textView release];
break;
default:
break;
}
[cellText release];
return cell;
}

I think the problem may actually be in my switch syntax . . . see anything?

kainjow
Jul 28, 2008, 01:01 PM
If you're customizing a table cell beyond simple text color/font/etc, you need to create a subclass. Apple has several examples on this on their iPhone Dev Center website.

grimjim
Aug 2, 2010, 04:09 AM
I suspect that the problem lies with your use of the reuse queue. You seem to have three "styles" of UITableViewCell, one in each section, yet you are only using a single reuse identifier for all cells in your table view. So there's a good chance that when a new cell is scrolled onto the screen, the cell that is dequeued and displayed will be the wrong type.

To test this, I suggest that you duplicate the UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
} block, and add it inside the case blocks for each section, using a different identifier in each case. Then, you should find that the relevant type of cell gets dequeued when the table is scrolled.