My UITableView is adding duplicate label on top of each other when I scroll up and down. So eventally so many labels get added that the add slows down and crash's.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *label;
label = [[UILabel alloc] initWithFrame:nameFrame];
[label setText:[name objectAtIndex:indexPath.row + indexPath.section]];
[label setFont:[UIFont fontWithName:#"Helvetica" size:18]];
[label setBackgroundColor:[UIColor clearColor]];
[cell addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:statusFrame];
[label setText:[status objectAtIndex:indexPath.row + indexPath.section]];
[label setFont:[UIFont fontWithName:#"Helvetica" size:18]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:(UITextAlignmentRight)];
[cell addSubview:label];
[label release];
return cell;
}
You are dequeuing reusable cells so the UILabel already exists on each dequeued cell. Try the following code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UILabel *label;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
label = [[UILabel alloc] initWithFrame:nameFrame];
label.tag = 1; //Important for finding this label
[label setText:[name objectAtIndex:indexPath.row + indexPath.section]];
[label setFont:[UIFont fontWithName:#"Helvetica" size:18]];
[label setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:statusFrame];
label.tag = 2; //Important for finding this label
[label setText:[status objectAtIndex:indexPath.row + indexPath.section]];
[label setFont:[UIFont fontWithName:#"Helvetica" size:18]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:(UITextAlignmentRight)];
[cell.contentView addSubview:label];
[label release];
}
else
{
label = (UILabel*)[cell.contentView viewWithTag:1];
label.text = [name objectAtIndex:indexPath.row + indexPath.section];
label = (UILabel*)[cell.contentView viewWithTag:2];
label.text = [status objectAtIndex:indexPath.row + indexPath.section];
}
return cell;
}
I did adjust the code to use the cell's contentView.
You need to simply remove the subview:
for (UIView * view in cell.contentView.subviews)
{
[view removeFromSuperview];
view = nil;
}
This will do the work "I had the same issue"
The way I solved this was not very elegant but worked.
The problem, as mentioned by Joe is that we are reusing the cells by dequeuing them. This means that sometimes we use a cell which already has its properties previously set, eg its textLabel. In my case it was due to differences in the cell structure. It was overlapping the imageview from one cell over another which shouldn't have an image at all.
I found that identifying the problematic parts and setting them to nil at the start of cellForRowAtIndex fixed the problem. This is the equivalent of wiping your cell slate clean before reusing it.
Here is an edited version of mine:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:bCellIdentifier];
cell.imageView.image = Nil;
cell.textLabel.text = Nil;
_nameField.text = #"";
...
// Setting the code with regards to the cells here
...
return cell;
}
Hope this helps
Related
I was trying to implement a UILabel into a cell and what I get is overlapping of some values when I scroll the table up and down a couple of times. I work with ARC so there is no release when I want, so my question is : What's the right way of implementing a Label into a tableView cell?
Here is how it looks
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1];
cell.textLabel.highlightedTextColor = [UIColor colorWithRed:0.753 green:0.651 blue:0.1569 alpha:1];
UILabel *cellLabelS1 = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, cell.frame.size.width, cell.frame.size.height)];
cellLabelS1.backgroundColor = [UIColor clearColor];
cellLabelS1.font = [UIFont boldSystemFontOfSize:16];
[cellLabelS1 setTextColor:[UIColor whiteColor]];
[cellLabelS1 setText:temperatureString];
temperatureString = nil;
[cell addSubview:cellLabelS1];
[[cell imageView]setImage:[UIImage imageNamed:imageFromCodeDay1]];
[[cell textLabel]setText:cityString];
return cell;
}
You should add a label to the cell only if you don't have one.If you reuse the cells on the second pass you add it again.
So my advice is to set a tag to the label and try to see if the cell contentView bass already the label. If not create and add it.
UILabel *myLabel = (UILabel *)[cell.contentView viewWithTag:2002];
if(!myLabel){
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 22)];
myLabel.tag = 2002;
[cell.contentView addSubview:myLabel];
}
myLabel.text = #"my new text";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *cellLabelS1 = [[UILabel alloc] initWithFrame:CGRectMake(250, 0,cell.frame.size.width, cell.frame.size.height)];
cellLabelS1.backgroundColor = [UIColor clearColor];
cellLabelS1.tag = 200;
cellLabelS1.font = [UIFont boldSystemFontOfSize:16];
[cellLabelS1 setTextColor:[UIColor whiteColor]];
[cell addSubview:cellLabelS1];
}
// Configure the cell...
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1];
cell.textLabel.highlightedTextColor = [UIColor colorWithRed:0.753 green:0.651 blue:0.1569 alpha:1];
UILabel *cellLabelS1 = (UILabel*)[cell viewWithTag:200];
[cellLabelS1 setText:temperatureString];
temperatureString = nil;
[[cell imageView]setImage:[UIImage imageNamed:imageFromCodeDay1]];
[[cell textLabel]setText:cityString];
return cell;
}
may this will help you....
Your problem is in these lines:
UILabel *cellLabelS1 = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, cell.frame.size.width, cell.frame.size.height)];
cellLabelS1.backgroundColor = [UIColor clearColor];
cellLabelS1.font = [UIFont boldSystemFontOfSize:16];
[cellLabelS1 setTextColor:[UIColor whiteColor]];
When you get a reused cell back from the table view, it already hass this label in it. What you can do to avoid adding duplicate labels is add it only if you need to allocate a new cell. However, this can make retrieving the label back from a reused cell quite complicated.
I would personally suggest creating a custom UITableViewCell in interface builder, and creating a custom UITableViewCell subclass that has a UILabel property.
Please see
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
UILabel *playerHeading=[[UILabel alloc ]initWithFrame:CGRectMake(5.0f, 10.0f, 70.0f, 30.0f)];
[playerHeading setBackgroundColor:[UIColor clearColor]];
NSString *str=[NSString stringWithFormat:#"Player %i",indexPath.row];
[playerHeading setText:str];
[playerHeading setFont:[UIFont boldSystemFontOfSize:15]];
if(indexPath.section%2==0){
playerHeading.textColor=[UIColor redColor];
}
else {
playerHeading.textColor=[UIColor blueColor];
}
UITextField *txt=[[UITextField alloc]initWithFrame:CGRectMake(80.0f, 13.0f, 170.0f, 30.0f)];
[txt setBackgroundColor:[UIColor clearColor]];
//[txt setBackgroundColor:[UIColor whiteColor]];
[txt setTextColor:[UIColor grayColor]];
txt.delegate=self;
//[txt setBorderStyle:UIBorderStyle ]
[txt setText:#"Enter Player name"];
//txt.layer.cornerRadius=8.0f;
// txt.layer.masksToBounds=YES;
//txt.layer.borderColor=[[UIColor redColor]CGColor];
// txt.layer.borderWidth= 1.0f;
[cell.contentView addSubview:txt];
[cell.contentView addSubview:playerHeading];
[cell.contentView setAlpha:0.8f];
//ce]ll.textLabel.text = [listData objectAtIndex:row];
}
return cell;
}
I want to display the number in sequence like Player 0, Player 1, Player 2...
but as I scroll the table i get numbers in random format like Player 1, Player 0 ,Player 3
Please help how to solve this issue.
You are adding the labels as new subviews all the time - this means you will have label above label above label, which is a waste of memory as well as a potential source of display issues.
When you create your cells (inside if (cell == nil) create and add the subviews at that point, and assign each one a tag. Then configure them all outside that loop. An example just with your playerHeading label:
UILabel *playerHeading;
if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
playerHeading=[[UILabel alloc ]initWithFrame:CGRectMake(5.0f, 10.0f, 70.0f, 30.0f)];
[playerHeading setBackgroundColor:[UIColor clearColor]];
[playerHeading setFont:[UIFont boldSystemFontOfSize:15]];
[playerHeading setTag:1];
[cell.contentView addSubview:playerHeading];
}
else
{
playerHeading = [cell.contentView viewWithTag:1];
}
playerHeading.text = [NSString stringWithFormat:#"Player %i",indexPath.row];
You do appear to have more than one section in your table (you have an if statement on indexPath.section) so your player numbers will start again from 0 in each section with the above code.
try to close the {} of the if(cell == nil) earlier like here:
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
i am new programmer of iphone App... i have 7 labels and 1 imageView in first cell of table view......
i write this code for that..... this work satisfactory...(may be it take times when scroll)
please tell me ...this is the right way to do this task or not....?
if not please...tell me ..the right way...
thanks in advance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"Cell %i",indexPath.section]];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:#"Cell %i",indexPath.section]] autorelease];
}
if(indexPath.row==0)
{
CGRect frame=CGRectMake(120,10, 80, 40);
UILabel *label1=[[UILabel alloc]init];
label1.frame=frame;
label1.text=#"first label";
[cell.contentView addSubview:label1];
[label1 release];
CGRect frame2=CGRectMake(200,10, 80, 40);
UILabel *label2=[[UILabel alloc]init];
label2.frame=frame2;
label2.text=#"second label";
[cell.contentView addSubview:label2];
[label2 release];
and so on.......
}
else if(indexPath.row==1)
{
//add four labels for this cell here......
}
return cell;
}
When you are reusing cell you don't need to create labels second time:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"Cell %i",indexPath.section]];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:#"Cell %i",indexPath.section]] autorelease];
if(indexPath.row==0)
{
CGRect frame=CGRectMake(120,10, 80, 40);
UILabel *label1=[[UILabel alloc]init];
label1.frame=frame;
label1.text=#"first label";
label1.tag = 1001;
[cell.contentView addSubview:label1];
[label1 release];
CGRect frame2=CGRectMake(200,10, 80, 40);
UILabel *label2=[[UILabel alloc]init];
label2.frame=frame2;
label2.text=#"second label";
label2.tag = 1002;
[cell.contentView addSubview:label2];
[label2 release];
and so on.......
}
}
if(indexPath.row==0)
{
UILabel *label1=[cell viewWithTag:1001];
label1.text=#"first label";
UILabel *label2=[cell viewWithTag:1002];
label2.text=#"second label";
and so on.......
}
return cell;
}
I'm accessing previously created labels using there tag value.
I think, it will be much easier to design such a complicated cell in InterfaceBuilder. If you're using Storyboards, you can design custom cells right away in your table view. If you're using xibs, you can create a nib that will have a custom UITableViewCell instead of the table view, UIViewController as owner and declare subclass of UITableViewCell in your project. That should make your sufferings much easier =)
I have a scrollable table.
In each cell I draw 3 UILabels. The first few cells draw ok. But as I scroll down the table the UILabels seems to be drawing over previous UILabels. Its like the Labels in the cell have an old label already there that was cleared. I could probably fix this by drawing a background color over the whole cell each redraw, but that still doesn't explain this strange problem and why its happening.
Anybody know why this occurs and what a solution might be?
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:indexPath.row];
UILabel * teamName1Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5, 100, 20)];
teamName1Label.textAlignment = UITextAlignmentCenter;
teamName1Label.textColor = [UIColor redColor];
teamName1Label.backgroundColor = [UIColor clearColor];
teamName1Label.text = aMatch.teamName1;
[cell addSubview:teamName1Label];
UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(115,5, 40, 20)];
teamVersusLabel.textAlignment = UITextAlignmentCenter;
teamVersusLabel.textColor = [UIColor redColor];
teamVersusLabel.backgroundColor = [UIColor clearColor];
teamVersusLabel.text = #"V";
[cell addSubview:teamVersusLabel];
UILabel *teamName2Label = [[UILabel alloc]initWithFrame:CGRectMake(155,5, 100, 20)];
teamName2Label.textAlignment = UITextAlignmentCenter;
teamName2Label.textColor = [UIColor redColor];
teamName2Label.backgroundColor = [UIColor clearColor];
teamName2Label.text = aMatch.teamName2;
[cell addSubview:teamName2Label];
return cell;
}
Many Thanks
-Code
I think the best solution for this problem is to define these labels in the
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath method
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
UILabel * teamName1Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5, 100, 20)];
teamName1Label.textAlignment = UITextAlignmentCenter;
teamName1Label.textColor = [UIColor redColor];
teamName1Label.backgroundColor = [UIColor clearColor];
teamName1Label.text = aMatch.teamName1;
teamName1Label.tag = 1;
[cell.contentView addSubview:teamName1Label];
[teamName1Label release];
UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(115,5, 40, 20)];
teamVersusLabel.textAlignment = UITextAlignmentCenter;
teamVersusLabel.textColor = [UIColor redColor];
teamVersusLabel.backgroundColor = [UIColor clearColor];
teamVersusLabel.text = #"V";
teamVersusLabel.tag = 2;
[cell.contentView addSubview:teamVersusLabel];
[teamVersusLabel release];
UILabel *teamName2Label = [[UILabel alloc]initWithFrame:CGRectMake(155,5, 100, 20)];
teamName2Label.textAlignment = UITextAlignmentCenter;
teamName2Label.textColor = [UIColor redColor];
teamName2Label.backgroundColor = [UIColor clearColor];
teamName2Label.text = aMatch.teamName2;
teamName2Label.tag = 3;
[cell.contentView addSubview:teamName2Label];
[teamName2Label release];
return cell;
}
Now the cellForRowAtIndexPath method will be--
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=nil;
static NSString *Identifier = #"Cell";
cell = [theTableView dequeueReusableCellWithIdentifier:Identifier];
if(cell == nil){
cell = [self reuseTableViewCellWithIdentifier:Identifier withIndexPath:indexPath];
}
Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:indexPath.row];
UILabel *label = (UILabel *)[cell.contentView viewWithTag:1];
label.text = aMatch.teamName1;
label = (UILabel *)[cell.contentView viewWithTag:2];
label.text = #"V";
label = (UILabel *)[cell.contentView viewWithTag:3];
label.text = aMatch.teamName2;
return cell;
}
Just try this code..Hope it helps :)
This is a cell re-use issue. The code is adding the labels to the cell every time cellForRowAtIndexPath is called (even if cell is not nil).
When you scroll, a previous cell that already has the labels is loaded and then the code adds new labels over the old ones.
Only create and add the labels to the cell in the if (cell == nil) section. When creating the labels, set tags on the labels.
Then outside the if, retrieve the labels using their tag using viewWithTag and set their text.
This is my cellForRowAtIndexPath function. I could not get the setText to the label to work. Can you please help me out?
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UILabel *messageLabel = nil;
int row = [indexPath row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, ROWHEIGHT) reuseIdentifier:CellIdentifier];
messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 5, 240, 60)];
[messageLabel setFont:[UIFont fontWithName:#"ArialMT" size:12]];
[messageLabel setTextColor:[UIColor blackColor]];
[messageLabel setBackgroundColor:[UIColor clearColor]];
[messageLabel setNumberOfLines:3];
[messageLabel setLineBreakMode:UILineBreakModeWordWrap];
[messageLabel setTag: messageTag];
[cell.contentView addSubview:messageLabel];
}
else{
messageLabel = (UILabel *)[cell viewWithTag:messageTag];
}
[messageLabel setText:[[[aSingleton wallItemArray] objectAtIndex:row] message]];
NSLog(#" -- > at row %d, message: %#", row, [[[aSingleton wallItemArray] objectAtIndex:row] message]);
return cell;
}
You're adding the UILabel to the cell's contentView, but asking the cell for the view.
Have you tried:
messageLabel = (UILabel *)[cell.contentView viewWithTag:messageTag];
EDIT: Also, you have two memory leaks - you're alloc'ing a UITableViewCell and a UILabel without (auto)releasing them anywhere.