Search code examples
iosnsindexpathuitableview

when i scroll tableview data is not displaying in proper way


I am building iOS App using StoryBoards.

I have a tableview.

In tableview Cell ,i created 5 buttons and a label programmatically.

In the button Action i increase the size of the selected button and make the other buttons as the default size.

For the 1st 4 cells it working fine.when i added new cell data is coming.

The problem occur when i scrolls the tableview,the buttons and labels in the cells are displaying not in the order.Wrong data is displayed.

cell state of the tableview is changing.

Here is my code.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *Cellidentifier1 = @"identifier";

    cell1 = [_skillSelectionTable dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];

    cell1.selectionStyle = UITableViewCellSelectionStyleNone;

 first[indexPath.row] = [UIButton buttonWithType:UIButtonTypeCustom];

            first[indexPath.row].frame = CGRectMake(75, 40, 40, 20);

            [first[indexPath.row] setTitle:@"first1" forState:UIControlStateNormal];

            [first[indexPath.row] setTitleColor:[UIColor colorWithRed:0.0/255.0f green:210.0/255.0f blue:186.0/255.0f alpha:1.0] forState:UIControlStateNormal];



            first[indexPath.row].backgroundColor=[UIColor colorWithRed:0.0/255.0f green:210.0/255.0f blue:186.0/255.0f  alpha:1.0];

            [first[indexPath.row] addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];

            first[indexPath.row].tag = indexPath.row;

            [cell1.contentView addSubview: first
[indexPath.row]];

           Second[indexPath.row] = [UIButton buttonWithType:UIButtonTypeCustom];

            Second[indexPath.row].frame = CGRectMake(120, 40, 40, 20);

            [Second[indexPath.row] setTitle:@"Second1
" forState:UIControlStateNormal];

            [Second[indexPath.row] setTitleColor:[UIColor colorWithRed:0.0/255.0f green:190.0/255.0f blue:176.0/255.0f alpha:1.0] forState:UIControlStateNormal];

            Second[indexPath.row].backgroundColor=[UIColor colorWithRed:0.0/255.0f green:190.0/255.0f blue:176.0/255.0f  alpha:1.0];

            [Second[indexPath.row] addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];

            Second[indexPath.row].tag = indexPath.row;

            [cell1.contentView addSubview:second[indexPath.row]];

           third[indexPath.row] = [UIButton buttonWithType:UIButtonTypeCustom];

            third[indexPath.row].frame = CGRectMake(160, 35, 50, 30);

            [third[indexPath.row] setTitle:@"third 
1" forState:UIControlStateNormal];

            [third[indexPath.row] setTitleColor:[UIColor colorWithRed:0.0/255.0f green:180.0/255.0f blue:166.0/255.0f alpha:1.0] forState:UIControlStateNormal];

            third[indexPath.row].backgroundColor=[UIColor colorWithRed:0.0/255.0f green:180.0/255.0f blue:166.0/255.0f  alpha:1.0];

            [third[indexPath.row] addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];

            third[indexPath.row].tag = indexPath.row;

            [cell1.contentView addSubview: third
[indexPath.row]];

            label[indexPath.row] = [[UILabel alloc]initWithFrame:CGRectMake(100, 69, 200, 21)];

             label[indexPath.row].text=@"second";

   label[indexPath.row].textAlignment=NSTextAlignmentCenter;

[cell1.contentView addSubview:label[indexPath.row]];

    return cell1;

}


-(IBAction)increaseAction:(id)sender

{

    UIButton *button = (UIButton *) sender;

    if ([[button currentTitle] isEqualToString:@“First1"])

    {

       First[button.tag].frame = CGRectMake(70, 35, 50, 30);

        second[button.tag].frame = CGRectMake(120, 40, 40, 20);

       third[button.tag].frame = CGRectMake(165, 40, 40, 20);

        label[button.tag].text=@"First";

}

    if ([[button currentTitle] isEqualToString:@"second1"])

    {

        First[button.tag].frame = CGRectMake(75, 40, 40, 20);

        second[button.tag].frame = CGRectMake(115, 35, 50, 30);

        third[button.tag].frame = CGRectMake(165, 40, 40, 20);

        label[button.tag].text=@"second";

    }

    if ([[button currentTitle] isEqualToString:@"third1"])

    {

        First[button.tag].frame = CGRectMake(75, 40, 40, 20);

        second[button.tag].frame = CGRectMake(120, 40, 40, 20);

        third[button.tag].frame = CGRectMake(160, 35, 50, 30);

        label[button.tag].text=@"third";

}

}

Solution

  • I found this issue is about cell reuse properly. So, edited your code and its working fine now.

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString * Cellidentifier1 = [NSString
                                        stringWithFormat:@"identifier %ld",(long)indexPath.row];
    
        cell1 = [_skillSelectionTable dequeueReusableCellWithIdentifier:Cellidentifier1];
    
        cell1.selectionStyle = UITableViewCellSelectionStyleNone;
    
     first[indexPath.row] = [UIButton buttonWithType:UIButtonTypeCustom];
    
                first[indexPath.row].frame = CGRectMake(75, 40, 40, 20);
    
                [first[indexPath.row] setTitle:@"first1" forState:UIControlStateNormal];
    
                [first[indexPath.row] setTitleColor:[UIColor colorWithRed:0.0/255.0f green:210.0/255.0f blue:186.0/255.0f alpha:1.0] forState:UIControlStateNormal];
    
    
    
                first[indexPath.row].backgroundColor=[UIColor colorWithRed:0.0/255.0f green:210.0/255.0f blue:186.0/255.0f  alpha:1.0];
    
                [first[indexPath.row] addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];
    
                first[indexPath.row].tag = indexPath.row;
    
                [cell1.contentView addSubview: first
    [indexPath.row]];
    
               Second[indexPath.row] = [UIButton buttonWithType:UIButtonTypeCustom];
    
                Second[indexPath.row].frame = CGRectMake(120, 40, 40, 20);
    
                [Second[indexPath.row] setTitle:@"Second1
    " forState:UIControlStateNormal];
    
                [Second[indexPath.row] setTitleColor:[UIColor colorWithRed:0.0/255.0f green:190.0/255.0f blue:176.0/255.0f alpha:1.0] forState:UIControlStateNormal];
    
                Second[indexPath.row].backgroundColor=[UIColor colorWithRed:0.0/255.0f green:190.0/255.0f blue:176.0/255.0f  alpha:1.0];
    
                [Second[indexPath.row] addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];
    
                Second[indexPath.row].tag = indexPath.row;
    
                [cell1.contentView addSubview:second[indexPath.row]];
    
               third[indexPath.row] = [UIButton buttonWithType:UIButtonTypeCustom];
    
                third[indexPath.row].frame = CGRectMake(160, 35, 50, 30);
    
                [third[indexPath.row] setTitle:@"third 
    1" forState:UIControlStateNormal];
    
                [third[indexPath.row] setTitleColor:[UIColor colorWithRed:0.0/255.0f green:180.0/255.0f blue:166.0/255.0f alpha:1.0] forState:UIControlStateNormal];
    
                third[indexPath.row].backgroundColor=[UIColor colorWithRed:0.0/255.0f green:180.0/255.0f blue:166.0/255.0f  alpha:1.0];
    
                [third[indexPath.row] addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];
    
                third[indexPath.row].tag = indexPath.row;
    
                [cell1.contentView addSubview: third
    [indexPath.row]];
    
                label[indexPath.row] = [[UILabel alloc]initWithFrame:CGRectMake(100, 69, 200, 21)];
    
                 label[indexPath.row].text=@"second";
    
       label[indexPath.row].textAlignment=NSTextAlignmentCenter;
    
    [cell1.contentView addSubview:label[indexPath.row]];
    
        return cell1;
    
    }
    
    
    -(IBAction)increaseAction:(id)sender
    
    {
    
        UIButton *button = (UIButton *) sender;
    
        if ([[button currentTitle] isEqualToString:@“First1"])
    
        {
    
           First[button.tag].frame = CGRectMake(70, 35, 50, 30);
    
            second[button.tag].frame = CGRectMake(120, 40, 40, 20);
    
           third[button.tag].frame = CGRectMake(165, 40, 40, 20);
    
            label[button.tag].text=@"First";
    
    }
    
        if ([[button currentTitle] isEqualToString:@"second1"])
    
        {
    
            First[button.tag].frame = CGRectMake(75, 40, 40, 20);
    
            second[button.tag].frame = CGRectMake(115, 35, 50, 30);
    
            third[button.tag].frame = CGRectMake(165, 40, 40, 20);
    
            label[button.tag].text=@"second";
    
        }
    
        if ([[button currentTitle] isEqualToString:@"third1"])
    
        {
    
            First[button.tag].frame = CGRectMake(75, 40, 40, 20);
    
            second[button.tag].frame = CGRectMake(120, 40, 40, 20);
    
            third[button.tag].frame = CGRectMake(160, 35, 50, 30);
    
            label[button.tag].text=@"third";
    
    }
    
    }
    

    Enjoy!!