【问题标题】:UITableView on scrolling goes wrongUITableView 滚动出错
【发布时间】:2017-05-02 07:40:18
【问题描述】:

我在 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;
}

【问题讨论】:

  • 尝试添加这一行 if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } 在 loadMoreTableViewCellForTableView

标签: ios objective-c uitableview


【解决方案1】:

你也应该使用这行代码:

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

在你的

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

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

【讨论】:

    【解决方案2】:

    如果我理解正确的话,cell.filterSelectionColor.alpha 在您滚动 TableView 时是不正确的,不是吗?

    您依赖于 Cell selected 属性,尽管单元格位置可能与滚动时创建时的位置不同。您应该将选定的单元格存储在其他位置(一个数组左右)并在 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;
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        在控制器中创建一个 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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-05
          • 2017-05-12
          • 1970-01-01
          • 2013-02-25
          • 1970-01-01
          相关资源
          最近更新 更多