Curnelious
Curnelious

Reputation: 1

Collapsable tableview bug

I have this bug with UITableView, there is one prototype cell i have made with storyboard, and its collapseable, which means when you hit the cell you get some more cells under that subject.

All the other cells under that subject, are having UIImageView as a side view. Whats happens is that after the first time i hit that subject cell, the image that should only be attached to the sub-cells, is also attached to the subject cell.

so, when i hit a cell i change this array to know its collapsable :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    //subject hitted
    if (indexPath.row == 0)
    {
        collapsedRows[indexPath.section]= ! collapsedRows[indexPath.section];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]withRowAnimation:UITableViewRowAnimationFade];
    }
    }

Than i tell the table view how many sections it has-now it has more because its opened :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    //subject cell is open
    if(collapsedRows[section])

    {
        if([[allAppointments objectAtIndex:section] count]>0)
        {

           NSMutableArray *sectionTime=[allAppointments objectAtIndex:section];

           return [sectionTime count]+1;

        }
        else
            return 1;
    }
    else
        return 1;


}

than, when reloading the table, i attach the image ONLY to the rows that are not 0 (=subject)

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];



   //labels on the cell
    UILabel *labelBusiness = (UILabel *)[cell viewWithTag:1];
    UILabel *labelTime = (UILabel *)[cell viewWithTag:3];
    UILabel *labelStreet = (UILabel *)[cell viewWithTag:4];
    UIImageView *sideView=(UIImageView*)[cell viewWithTag:6];
    UILabel *labelTitle = (UILabel *)[cell viewWithTag:8];
    labelBusiness.text=@"";
    labelStreet.text=@"";
    labelTime.text=@"";
    labelTitle.text=@"";



    // ONLY WHEN SUBJECT SHOW THE SUBJECT TITLE-NO IMAGE HERE!
    if (indexPath.row==0)
    {

        NSString *title=@"";
        if(indexPath.section==0)
            title=@"PAST";
        else if(indexPath.section==1)
             title=@"TODAY";
        else if(indexPath.section==2)
            title=@"TOMORROW";
        else if(indexPath.section==3)
            title=@"FUTURE";

        labelTitle.text=title;

    }



    //ON THE SUB CELLS ONLY SHOW IMAGES
    else
    {

              //do more stuff here….

               //show images
               if( [allImages count]>0)
               {
                 cell.imageView.layer.masksToBounds = YES;
                 cell.imageView.layer.cornerRadius = 12.0;
                 cell.imageView.image=[[allImages objectAtIndex:indexPath.section] objectAtIndex:indexPath.row-1] ;

               }


           }

    }

    return cell;
}

So after the first time i hit the row and open the sub-cells, than close them, from now on the image is attached also the the subject cells ! Logging don't show that its even enter the else condition.

Upvotes: 0

Views: 77

Answers (1)

user3386109
user3386109

Reputation: 34839

When cells come out of the reuse queue, they have all of the old information in them. So you need to set the cell.imageView.image to nil to get rid of the image when you setup a subject cell.

Upvotes: 2

Related Questions