Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-25-2009, 01:10 AM   #1 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 27
iPhoneDeveloper is on a distinguished road
Default UITableViewCell not getting refreshed

Hi,

I have a UITableViewCell with a label in it.Below is the code to create it.
But when I try to reload the cells with different label text i still see the old text behind it and it is kind of overlapping on each other.Any ideas how to clear the previous text?

UITableViewCell *cell;
// Set up the cell
cell = [tableView dequeueReusableCellWithIdentifier:@"my-cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:rect reuseIdentifier:@"param-cell"] autorelease];
}
UILabel *label = [[UILabel alloc] initWithFrame:rect];
label.text=@"my text";
[cell.contentView addSubview:label];

Thanks.
iPhoneDeveloper is offline   Reply With Quote
Old 05-26-2009, 03:46 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2008
Posts: 355
Bucky is an unknown quantity at this point
Default

It's because you're creating a new label every time the cell is redrawn. What you can't see is that every time you scroll, a new label is being chucked on top of the old one, so there could be hundreds of labels in that cell, even if you haven't changed the text. If you're using a large table, I'm guessing you're scrolling performance isn't great either, since you're calling alloc a lot (which is an expensive thing to do).

I can see that you're using reusable cells and queuing them for better performance (which is the correct thing to do), but the idea of this is to prevent memory continually being allocated, and therefore using less cpu cycles (for better performance). By allocating hundreds of labels, you're actually making that a pointless exercise.

Luckily, the standard UITableViewCell already has a built-in UILabel as a subview.

Therefore to add text, you just need to do this:
Code:
 cell.text = @"My Text";
If you need to change the subviews, you should use a custom subclass of UITableViewCell instead.
Bucky is offline   Reply With Quote
Old 05-27-2009, 01:43 AM   #3 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 27
iPhoneDeveloper is on a distinguished road
Cool

Thanks for the reply,I had to add UILabel because I wanted to change the Font and Font size of the text on UiTableViewCell.Is there a way to change the font without adding label?

Quote:
Originally Posted by Bucky View Post
It's because you're creating a new label every time the cell is redrawn. What you can't see is that every time you scroll, a new label is being chucked on top of the old one, so there could be hundreds of labels in that cell, even if you haven't changed the text. If you're using a large table, I'm guessing you're scrolling performance isn't great either, since you're calling alloc a lot (which is an expensive thing to do).

I can see that you're using reusable cells and queuing them for better performance (which is the correct thing to do), but the idea of this is to prevent memory continually being allocated, and therefore using less cpu cycles (for better performance). By allocating hundreds of labels, you're actually making that a pointless exercise.

Luckily, the standard UITableViewCell already has a built-in UILabel as a subview.

Therefore to add text, you just need to do this:
Code:
 cell.text = @"My Text";
If you need to change the subviews, you should use a custom subclass of UITableViewCell instead.
iPhoneDeveloper is offline   Reply With Quote
Old 05-27-2009, 02:50 AM   #4 (permalink)
Registered Member
 
Gomfucius's Avatar
 
Join Date: May 2009
Location: San Francisco
Posts: 47
Gomfucius is on a distinguished road
Default

For customizing the text for UITableViewCell, I think they changed the API a little bit from Beta 5.
Now they use textLabel instead of just text.

cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:aInt];
Gomfucius is offline   Reply With Quote
Old 05-30-2009, 02:01 AM   #5 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 27
iPhoneDeveloper is on a distinguished road
Default

Quote:
Originally Posted by Gomfucius View Post
For customizing the text for UITableViewCell, I think they changed the API a little bit from Beta 5.
Now they use textLabel instead of just text.

cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:aInt];
I am using simulator version 2.2.1,I do not think this API is supported in that version.
iPhoneDeveloper is offline   Reply With Quote
Old 10-05-2011, 12:05 AM   #6 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 1
djmason9 is on a distinguished road
Default

Quote:
Originally Posted by iPhoneDeveloper View Post
Hi,

I have a UITableViewCell with a label in it.Below is the code to create it.
But when I try to reload the cells with different label text i still see the old text behind it and it is kind of overlapping on each other.Any ideas how to clear the previous text?

UITableViewCell *cell;
// Set up the cell
cell = [tableView dequeueReusableCellWithIdentifier:@"my-cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:rect reuseIdentifier:@"param-cell"] autorelease];
}
UILabel *label = [[UILabel alloc] initWithFrame:rect];
label.text=@"my text";
[cell.contentView addSubview:label];

Thanks.
put your label code inside the if (cell == nil) and it will only get called once.
djmason9 is offline   Reply With Quote
Old 01-17-2012, 07:54 PM   #7 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 42
xcoderx is on a distinguished road
Default

Quote:
Originally Posted by Bucky View Post
It's because you're creating a new label every time the cell is redrawn. What you can't see is that every time you scroll, a new label is being chucked on top of the old one, so there could be hundreds of labels in that cell, even if you haven't changed the text. If you're using a large table, I'm guessing you're scrolling performance isn't great either, since you're calling alloc a lot (which is an expensive thing to do).

I can see that you're using reusable cells and queuing them for better performance (which is the correct thing to do), but the idea of this is to prevent memory continually being allocated, and therefore using less cpu cycles (for better performance). By allocating hundreds of labels, you're actually making that a pointless exercise.

Luckily, the standard UITableViewCell already has a built-in UILabel as a subview.

Therefore to add text, you just need to do this:
Code:
 cell.text = @"My Text";
If you need to change the subviews, you should use a custom subclass of UITableViewCell instead.
ARE you "the" bucky? like thenewboston on youtube?
xcoderx is offline   Reply With Quote
Reply

Bookmarks

Tags
refresh, uitableviewcell

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 339
6 members and 333 guests
givensur, ipodphone, jbro, mer10, n00b, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,881
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:09 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0