UITableView and UILabel repeating

This has just completely stomped me.

I have a custom UIView and UILable that I have added to my UITableView in cell.contentView as a subview. I have an array called arryData with about 100 strings as data in it that I want to show in my table. The problem is that when table is created I see the first 0-4 rows from my arryData with values and if I keep scrolling down the table, all I see are the first 5 strings repeating over and over. What am I doing wrong? here is my code.

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    //NSLog(@"Inside cellForRowAtIndexPath");

    static NSString *CellIdentifier = @"Cell";

    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UILabel *labelValue1 = [[UILabel alloc] initWithFrame:CGRectMake(70, 25, 200, 30)];

    // If no cell is available, create a new one using the given identifier.
    if (cell == nil)
        // Use the default cell style.
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        UIImageView *imgVw1 = [[UIImageView alloc] initWithFrame:CGRectMake(11, 0, 300, 75)];
        imgVw1.image = [UIImage imageNamed:@"background_pic5.png"];
        imgVw1.userInteractionEnabled = YES;
        imgVw1.exclusiveTouch = YES;
        [cell.contentView addSubview:imgVw1];

        labelValue1.text = [arryData objectAtIndex:indexPath.row];
        labelValue1.font = [UIFont fontWithName:@"BradleyHandITCTT-Bold" size: 22.0];
        labelValue1.textColor = [UIColor whiteColor];
        labelValue1.textAlignment = UITextAlignmentCenter;
        labelValue1.numberOfLines = 1;
        labelValue1.backgroundColor = [UIColor clearColor];
        labelValue1.adjustsFontSizeToFitWidth = YES;
        labelValue1.minimumFontSize = 10.0;
        labelValue1.tag = (indexPath.row)+100;
        [cell.contentView addSubview:labelValue1];

        NSLog(@"indexPath.row: %d - arrayData: %@..." , indexPath.row, [arryData objectAtIndex:indexPath.row]);
        NSLog(@"indexPath.row: %d - arrayData: %@..." , indexPath.row, [arryData objectAtIndex:indexPath.row]);

        labelValue1 = (UILabel *) [cell viewWithTag:((indexPath.row)+100)];
        labelValue1.text = [arryData objectAtIndex:indexPath.row];

    //Do this only if row has a value. For blank ones, Skip this

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
        //its iphone
        cell.textLabel.font = [UIFont systemFontOfSize:13];

    if([arryDataMutable containsObject:indexPath])
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [cell setAccessoryType:UITableViewCellAccessoryNone];

    //cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;


This is what my output looks like when I scroll down the table and all I keep seeing is text in my labelValue1 from indexPath.row 0 - 4 repeat over and over

indexPath.row: 0 - arrayData: Behind Whipped...
indexPath.row: 1 - arrayData: Aftershock Whip...
indexPath.row: 2 - arrayData: Bull Whipped Sound 1...
indexPath.row: 3 - arrayData: Bull Whipped Sound 2...
indexPath.row: 4 - arrayData: Bullet Fire Whip...
indexPath.row: 5 - arrayData: Circus Whip...
indexPath.row: 6 - arrayData: Circus Whip2...


Change the way you are setting tag in this case,

labelValue1.tag = 100;

and read it as,

labelValue1 = (UILabel *) [cell viewWithTag:100];

Since you are dequeuing the cells, it will try to reuse the cell and label is added to that when you are allocating memory for the cell. When the else part is executed, you need to access the same label which was already added as [cell viewWithTag:100];.

[cell viewWithTag:((indexPath.row)+100)]; will return nil for most of the cells which were not visible for the first time. Since you are reusing cells, you will see the same first 5 visible cells again with same text. In this case, you were trying to set text to a nil object. So it wont make any difference and hence you will see the same text repeating.

Need Your Help

Managing dependencies to multiple versions of a library in Java and Eclipse

java jar versioning

In the Eclipse environment I have project A. A has dependencies to projects or libraries B and C. (does not make a difference if they are projects or libraries)

Android app and java activities, issues with onCreate() method and XML

java android xml android-activity

I am trying use the onClick() function in an activity for an android app. So far I have:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.