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];
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.
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
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.
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.
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?