简体   繁体   English

滚动时的UITableView出错

[英]UITableView on scrolling goes wrong

I have created UITableView on UIView Class. 我在UIView类上创建了UITableView。

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. 基于选择行,将UIImageView的alpha值设置为1.0f,然后取消选择将alphaImage设置为0.2f的行,效果很好。

But on scrolling the selected value (ie alpha 1.0f) is highlighted with wrong cell, which was not selected at all. 但是在滚动时,选定的值(即alpha 1.0f)会以错误的单元格突出显示,而该单元格根本没有被选中。

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? 如果我对您的理解正确,那么当您在TableView中滚动时,cell.filterSelectionColor.alpha是不正确的,不是吗?

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. 您应该将所选单元格存储在其他位置(大约一个数组),然后在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. 重新加载tableView时,它使用相同的单元格实例并更改数据。 When you reload/scroll your cellForRowAtIndexpath method is called and over there you will have to specify which cell should have which alpha. 当您重新加载/滚动时,将调用cellForRowAtIndexpath方法,并在那儿必须指定哪个单元格应具有哪个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 在控制器中创建一个int属性

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.. 由于tableView重用了单元格,因此您需要通过cellForRow方法设置单元格。

- (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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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