Ios UITableView在滚动时出错

Ios UITableView在滚动时出错,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我已经在UIView类上创建了UITableView 基于选择行,将UIImageView的alpha值设置为1.0f 在上取消选择行,将alpha值设置为0.2f,效果良好 但在滚动时,所选值(即alpha 1.0f)会用错误的单元格突出显示,而该单元格根本没有被选中 请查找我已实现的以下代码。 我们将非常感谢您的反馈 //代码 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;

我已经在UIView类上创建了UITableView

基于选择行,将UIImageView的alpha值设置为1.0f 在上取消选择行,将alpha值设置为0.2f,效果良好

但在滚动时,所选值(即alpha 1.0f)会用错误的单元格突出显示,而该单元格根本没有被选中

请查找我已实现的以下代码。 我们将非常感谢您的反馈

//代码

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

您还应使用以下代码行:

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

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


因为它会重复使用单元格,这就是为什么您以前的设置不会重置的原因。

如果我理解正确,cell.filterSelectionColor.alpha在您滚动浏览TableView时就不正确,不是吗

您依赖于Cell selected属性,但单元格位置可能与滚动时创建的单元格位置不同。您应该将选定的单元格存储在其他位置(数组等),并在CellForRowatineXpath中刷新单元格状态。类似于:

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

当tableView重新加载时,它使用相同的单元格实例并更改数据。当您重新加载/滚动
cellForRowAtIndexpath
方法时,将调用该方法,您必须在那里指定哪个单元格应具有哪个alpha。下面需要更改代码:

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

在控制器中创建int属性

NSMutableArray *selectedCells;
初始化变量

- (void)viewDidload {
...
...
selectedCells = [NSMutableArray array];
}
在选择和取消选择时设置值

- (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];
}
}
尝试在loadMoreTableViewCellForTableView中的if(cell==nil){cell=[[uitableViewCellAlloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}中添加此行
- (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;
}