0

I have created blur view in view controller and added sub view of custom table view cell. The problem is if I select 1st cell, it works perfectly. But if I select 2nd cell, blur view is shown in 3rd cell. If I select 3rd cell, blur view is shown in 5th cell.

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TableViewCell"];
    cell = [TableViewCell new];
    [self resetBoolArray];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    cell = (TableViewCell *) [self.tableView dequeueReusableCellWithIdentifier:@"TableViewCell"];
    self.effectView.frame = cell.frame;
    [self.effectView setHidden:[[self.boolAry objectAtIndex:indexPath.row] boolValue]];
    [cell addSubview:self.effectView];    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self resetBoolArray:indexPath.row];
    [self.tableView reloadData];
    NSLog(@"selected indexpath %ld",indexPath.row);
}

- (void)resetBoolArray {
    self.boolAry = [NSMutableArray new];
    for (int i = 0; i < 6; i++) {
        [self.boolAry addObject:[NSNumber numberWithBool:YES]];
    }
}

- (void)resetBoolArray: (NSInteger)indexpath {
    self.boolAry = [NSMutableArray new];
    for (int i = 0; i < 6; i++) {
        if(i == indexpath) {
            [self.boolAry addObject:[NSNumber numberWithBool:NO]];}
        else{
            [self.boolAry addObject:[NSNumber numberWithBool:YES]];}
    }
}
j.f.
  • 3,908
  • 2
  • 29
  • 42
erb dinesh
  • 101
  • 1
  • 8
  • try add the first line [self.effectView removeFromSuperView] in function tableView:cellForRowAtIndexPath: – gaRik Oct 15 '15 at 15:21
  • You shouldn't add a new blur view every time you dequeue a cell, add it once when the cell is created and hide/show it as needed. Do this in your table cell subclass and expose a property to hide/show the blur view. – dan Oct 15 '15 at 15:33
  • Thank you guys for suggested answers. I got solution from other way. I fixed blur view in custom cell. Now its working fine – erb dinesh Oct 16 '15 at 06:20

0 Answers0