简体   繁体   中英

UITableView on scrolling goes wrong

I have created UITableView on UIView Class.

Based on the selection row setting the alpha value of UIImageView to 1.0f on deselect the row setting alpha value to 0.2f, which work good.

But on scrolling the selected value (ie alpha 1.0f) is highlighted with wrong cell, which was not selected at all.

Please find the below code which i have implemented. Your feedback will be appreciated.

// Code

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1; 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [colorNameList count]; // count is century.
}

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

    return [self loadMoreTableViewCellForTableView:tableView indexPath:indexPath];

}

- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {

    FilterColorTableViewCell *cell = (FilterColorTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FilterColorTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    [cell.filterColorImageView setBackgroundColor:[UIColor colorWithHexString:[[[ColorModelClass colorListNames]allValues] objectAtIndex:indexPath.row]alpha:1]];
    cell.lbl_FilterColorName.text = [colorNameList objectAtIndex:indexPath.row];
    cell.lbl_FilterColorCount.text = [NSString stringWithFormat:@"Items: %ld",(long)indexPath.row];

    return cell;
}

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

    FilterColorTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    self.filterColorTableView.allowsMultipleSelection = YES;
    cell.filterSelectionColor.alpha = (cell.selected ?1.0f:0.2f);

}

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

    FilterColorTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.filterSelectionColor.alpha = (cell.selected ?1.0f:0.2f);
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

You should also use this line of code:

cell.filterSelectionColor.alpha = (cell.selected ?1.0f:0.2f);

in your

- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {

because it reuses the cell and this is why your previous settings are not reset.

If I understood you correctly, cell.filterSelectionColor.alpha is not correct as soon as you scroll through the TableView, isn't it?

You are relying on the Cell selected property, though cell position may not be the same as when they were created when scrolling. You should store the selected cells somewhere else (an array or so) and refresh Cell's status in cellForRowAtIndexPath. Something like:

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

    return [self loadMoreTableViewCellForTableView:tableView indexPath:indexPath];

}

- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {

    FilterColorTableViewCell *cell = (FilterColorTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FilterColorTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    [cell.filterColorImageView setBackgroundColor:[UIColor colorWithHexString:[[[ColorModelClass colorListNames]allValues] objectAtIndex:indexPath.row]alpha:1]];
    cell.lbl_FilterColorName.text = [colorNameList objectAtIndex:indexPath.row];
    cell.lbl_FilterColorCount.text = [NSString stringWithFormat:@"Items: %ld",(long)indexPath.row];

    // selectedItems is an array of booleans with as many elements as the TableView
    cell.filterSelectionColor.alpha = self.selectedItems[indexPath.row] ? 1.0f : 0.2f;

    return cell;
}

When tableView reloads it uses the same cell instance and changes the data. When you reload/scroll your cellForRowAtIndexpath method is called and over there you will have to specify which cell should have which alpha. Below change in your code is required :

- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath  {

    FilterColorTableViewCell *cell = (FilterColorTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FilterColorTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.filterColorImageView setBackgroundColor:[UIColor colorWithHexString:[[[ColorModelClass colorListNames]allValues] objectAtIndex:indexPath.row]alpha:1]];
    cell.lbl_FilterColorName.text = [colorNameList objectAtIndex:indexPath.row];
    cell.lbl_FilterColorCount.text = [NSString stringWithFormat:@"Items: %ld",(long)indexPath.row];

    // Set alpha here
    cell.filterSelectionColor.alpha = (cell.selected ?1.0f:0.2f);
    return cell;
}

create an int property in your controller

NSMutableArray *selectedCells;

initialize the variable..

- (void)viewDidload {
...
...
selectedCells = [NSMutableArray array];
}

set the value on selection and deselection..

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
...
...
[selectedCells addObject:[NSNumber numberWithInt:indexPath.row]];
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
...
...
for (int i=0;i<selectedCells.count;i++) {
    if([selectedCells[i] intValue] == indexPath.row)
        [selectedCells removeObjectAtIndex:i];
}
}

As the cells are reused by the tableView.. you need to setup the cell from the cellForRow method..

- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {

    FilterColorTableViewCell *cell = (FilterColorTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FilterColorTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

...
...
BOOL selected = [selectedCells containsObject:[NSNumber numberWithInt:indexPath.row]];
cell.filterSelectionColor.alpha = (selected) ?1.0f:0.2f;

    return cell;
}

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