简体   繁体   中英

Custom UITableviewCell content refresh while scrolling

My Custom tableview cell content getting empty after scrolling.So pls help me with this.

My custom cell has 8 buttons and a label.First I'm showing only label that has title and on selection I'm expanding the cell and showing all buttons.So, when I select few buttons and I do scrolling,buttons that I selected getting refreshed or get back to normal state.Here is the code.Pls help me with this

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

    static NSString *simpleTableIdentifier = @"newFilterCell";

    newFilterCell *cell = (newFilterCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"newFilterCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        [cell.contentView.superview setClipsToBounds:YES];
    }


     cell.QuestionLabel.text=[orderArray objectAtIndex:indexPath.row];

    NSArray * arr = [filterInfo objectForKey:[orderArray objectAtIndex:indexPath.row]];
    int val = 0;
     NSLog(@"%@",cell.subviews);

    NSArray * cellViews = cell.contentView.subviews;
    if (arr.count>0)
    {
        for (int i=1; i<=8; i++) {
            if (i<=arr.count) {
                UIButton * target = (UIButton*)[cell viewWithTag:i];;
                [target setTitle:[arr objectAtIndex:i-1]forState:UIControlStateNormal];

                [target addTarget:self action:@selector(selectButton:) forControlEvents:UIControlEventTouchUpInside];


            }
            else
            {
                [[cell viewWithTag:i] setHidden:YES];
            }

        }

    }

   [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;


    }

On selection of my cell I'm expanding and reloading the tableview cells.

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

        _expandIndexPath = nil;
         NSMutableArray *modifiedRows = [NSMutableArray array];
         [tableView reloadRowsAtIndexPaths:modifiedRows withRowAnimation:UITableViewRowAnimationLeft];
    }
    else
    {
        selectedRow=indexPath.row;
        NSMutableArray *modifiedRows = [NSMutableArray array];

        [tableView deselectRowAtIndexPath:indexPath animated:TRUE];
        _expandIndexPath = indexPath;
        [modifiedRows addObject:indexPath];

        [tableView reloadRowsAtIndexPaths:modifiedRows withRowAnimation:UITableViewRowAnimationLeft];


}

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSArray * arr = [filterInfo objectForKey:[orderArray objectAtIndex:indexPath.row]];

    if ([indexPath isEqual:_expandIndexPath])

    {
        if ([[orderArray objectAtIndex:indexPath.row]isEqualToString:@"Height"]||[[orderArray objectAtIndex:indexPath.row]isEqualToString:@"Age"])
        {

            return 275;
        }
    else
    {

        if(arr.count==3)
        return 55*arr.count;
    else
        return 37*arr.count;
    }
    }
    else
    return 70;
}

UITableViewCells are getting recycled. That's why its not safe to do it your way. Your data model needs to remember your buttons and other stuff that changed. You need to apply the changes every time the cell gets created.

You need to check in the cellForRowAtIndexPath what button is pressed and then show the view correctly.

You need to remember what happend in the cells with an external data source to apply the changes you want.

In your cellForRowAtIndexPath should be something like a check for a boolean whether or not you show some stuff:

if(button_is_pressed_for_row_5 == true){
     button_in_cell.hidden = true;
}else{
     button_in_cell.hidden = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM