繁体   English   中英

滚动时的UITableView出错

[英]UITableView on scrolling goes wrong

我在UIView类上创建了UITableView。

基于选择行,将UIImageView的alpha值设置为1.0f,然后取消选择将alphaImage设置为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 {

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

如果我对您的理解正确,那么当您在TableView中滚动时,cell.filterSelectionColor.alpha是不正确的,不是吗?

尽管单元格的位置可能与滚动时创建单元格的位置不同,但您依赖于“单元格”选定的属性。 您应该将所选单元格存储在其他位置(大约一个数组),然后在cellForRowAtIndexPath中刷新单元格的状态。 就像是:

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

由于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