Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

namanhams

macrumors regular
Original poster
Jun 3, 2009
153
0
I have a UITableView, and for each cell i add a UITextView to the content view.
Then i set the backgroundColor of the UITextView to clearColor in order to make the corner of the table rounded instead of 90 degrees.

But then funny things happen. After i scroll up and down the table for a while, the text inside the cell begins overlaying. I can not read the text anymore.

I also had this problem when i try to draw the PDF page. The text in the pages begins overlaying after a while. But after i use CGContextSetGrayFillColor, everything works perfect. Actually i dont know why, i just Google.

Any idea would be appreciated.
Thanks.
 
Are you adding the UITextView every time the cellForRowAtIndexPath is called or only in the initialization part of that method? If the former, depending on how the cell is reused, you could end up with the textView from a previous use of the cell remaining and a new textView added as well. Add the textView once per cell, in the initialization piece, and then just set the text of it every time.
 
Hi Dejo,

Thank for your reply. I just realize that i add a UITextView to the content view of the cell everytime in the cellForRowAtIndexPath and that's memory leak.
I also manage to fix the problem.

But now there's another problem. The cells seem to display correct data, but somtimes suddenly some cells dont display its data but display data of other cell. If i drag the table arround, then everything becomes fine for a while, and then got problem again. I feel that, everytime when a row goes out of the screen, it's cell is put into the queue, and a new row comes into the screen and take the cell from the queue. Although i set the data for the new row, the new row seems to use the old data of the cell, may be just because the display is not updated. I dont know why. But this problem only happens to some specific rows. Other rows always display correct data.
 
Hi,

Here's the code. Thanks again.

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
		cell.autoresizesSubviews = YES;
		cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
		cell.contentView.autoresizesSubviews = YES;
		cell.contentView.bounds = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
		cell.contentView.clipsToBounds = YES;
		
		UITextView *textView = [[UITextView alloc] initWithFrame:cell.contentView.bounds];
		[textView autorelease];
		textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
		textView.editable = NO;
		textView.scrollEnabled = NO;
		textView.backgroundColor = [UIColor clearColor];
		NSString *created_at = [[person.status objectAtIndex:indexPath.row] objectForKey:@"created_at"];
		NSString *text = [[person.status objectAtIndex:indexPath.row] objectForKey:@"text"];
		textView.text = [NSString stringWithFormat:@"%@\n%@", created_at, text];
		textView.bounds = CGRectMake(0, 0, textView.contentSize.width, textView.contentSize.height);
		[cell.contentView addSubview:textView];
	}
	else
	{
		UITextView *textView = (UITextView *) [cell.contentView.subviews objectAtIndex:0];
		NSString *created_at = [[person.status objectAtIndex:indexPath.row] objectForKey:@"created_at"];
		NSString *text = [[person.status objectAtIndex:indexPath.row] objectForKey:@"text"];
	        textView.text = [NSString stringWithFormat:@"%@\n%@", created_at, text];
	}
	
	return cell;
}
 
Hi,

I just move these line of code :

Code:
UITextView *textView = (UITextView *) [cell.contentView.subviews objectAtIndex:0];
NSString *created_at = [[person.status objectAtIndex:indexPath.row] objectForKey:@"created_at"];
NSString *text = [[person.status objectAtIndex:indexPath.row] objectForKey:@"text"];
textView.text = [NSString stringWithFormat:@"%@\n%@", created_at, text];

to the delegate method :
Code:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

and now everything works nearly perfect. Still have the problem, but it's very rare.
 
This code isn't right. Do it this way:

Code:
#define TEXT_TAG 100

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
//		cell.autoresizesSubviews = YES;
		cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//		cell.contentView.autoresizesSubviews = YES;
//		cell.contentView.bounds = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
//		cell.contentView.clipsToBounds = YES;
		
		UITextView *textView = [[UITextView alloc] initWithFrame:cell.contentView.bounds];
		textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
		textView.editable = NO;
		textView.scrollEnabled = NO;
		textView.backgroundColor = [UIColor clearColor];
textView.tag = TEXT_TAG;
		[cell.contentView addSubview:textView];
[textView release];
	}

		UITextView *textView = (UITextView *) [cell.contentView viewWithTag:TEXT_TAG];
		NSString *created_at = [[person.status objectAtIndex:indexPath.row] objectForKey:@"created_at"];
		NSString *text = [[person.status objectAtIndex:indexPath.row] objectForKey:@"text"];
	        textView.text = [NSString stringWithFormat:@"%@\n%@", created_at, text];
	
	return cell;
}

I added a few lines that you need and commented out a few lines that I think are useless.
 
Hi PhoneyDeveloper,

Thanks for your comment. I dont know that the autoresizeSubviews property of a cell and its contentView is default YES.

Anw, i still got the problem that sometimes a cell suddenly display the text of other cell, although it happens very rare. It's like sometimes the cell can not update its text.
 
Thanks for your comment. I dont know that the autoresizeSubviews property of a cell and its contentView is default YES.

Pretty sure it is. I never have set that property and all my cells resize their subviews.

Anw, i still got the problem that sometimes a cell suddenly display the text of other cell, although it happens very rare. It's like sometimes the cell can not update its text.

All I can suggest is try to debug it. Maybe there's a problem with the source of the text. With the code I showed it should always correctly set the text for a given row.

Why do you use a textview rather than a label. If the text is only two lines and can't scroll they should be the same. What version of the OS are you testing this on? There were some problems with textview in 2.x that I think are fixed on 3.0.
 
Pretty sure it is. I never have set that property and all my cells resize their subviews.



All I can suggest is try to debug it. Maybe there's a problem with the source of the text. With the code I showed it should always correctly set the text for a given row.

Why do you use a textview rather than a label. If the text is only two lines and can't scroll they should be the same. What version of the OS are you testing this on? There were some problems with textview in 2.x that I think are fixed on 3.0.


I use textView because the text is multiple line and it's not static text.
I use the version 2.2.1.

Here's another thing i see : when a cell display wrong text, i click on the cell, the cell get focused, change background color to blue and set the correct text.
I believe there's nothing wrong with the source text, because i use NSLog to debug and it's correct. It's just the cell sometimes doesn't update itself.
 
Maybe because you are using a UITextView, it's not getting notified to update its display during the table-cell update. Perhaps try a [textView setNeedsDisplay].
 
I believe that what you're describing is a bug in UITextView.

I recommend that you use UILabel instead. It can display multiline text.
 
@ Dejo : i use setNeedsDisplay but it doesn't solve the problem
@ PhoneyDeveloper : i use the UILable and i manage to solve the problem. Thanks a lot.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.