代码之家  ›  专栏  ›  技术社区  ›  Alex Saidani

UITableView嵌入UIContainerView单元重用

  •  0
  • Alex Saidani  · 技术社区  · 11 年前

    我有一个支持滚动的UITableViewController,嵌入在UIContainerView中。在表视图中有一个自定义的UITableViewCell,它内部有一个UIImageView,当您在表视图的多个单元格中选择一个时,我会获取索引路径,并将所选单元格的图像视图设置为显示勾号。表视图由数组中的多条数据动态填充。易于理解的

    问题是,因为表视图的长度超过了容器视图中可见的长度,所以索引路径似乎只适用于可见内容。这意味着它正在将具有相应索引路径的单元格设置为具有勾号的可见单元格。

    假设五个单元格可见,则选择第四个。你再往下走五个单元格,第十个单元格上有一个勾号,但从技术上讲,这是不可见内容的第五个单元格。我认为这与细胞再利用有关,但我不确定该怎么做。

    如何解决这个问题?(代码如下)

        - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CuisineTableCell *cell  = [tableView cellForRowAtIndexPath:indexPath];
        //Store current view in defaults
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
        if (cuisineSelected == NO) {
            cuisineSelected = YES;
            cell.imageView.image = [UIImage imageNamed:@"TickedCircle"];
            //Set cuisine one defaults
            [defaults setObject:[cuisinesArray objectAtIndex:indexPath.row] forKey:@"filterCuisineString"];
            [defaults synchronize];
            selectedRow = [tableView indexPathForSelectedRow];
        }
        else if(indexPath.row == selectedRow.row) {
            cuisineSelected = NO;
            cell.imageView.image = [UIImage imageNamed:@"UntickedCircle"];
            //Set cuisine one defaults
            [defaults setObject:@"" forKey:@"filterCuisineString"];
            [defaults synchronize];
            selectedRow = NULL;
        }
        else {
            CuisineTableCell *selectedCell  = [tableView cellForRowAtIndexPath:selectedRow];
            selectedCell.imageView.image = [UIImage imageNamed:@"UntickedCircle"];
            // the one and only cell in the section
            cuisineSelected = YES;
            cell.imageView.image = [UIImage imageNamed:@"TickedCircle"];
            //Set cuisine one defaults
            [defaults setObject:[cuisinesArray objectAtIndex:indexPath.row] forKey:@"filterCuisineString"];
            [defaults synchronize];
            selectedRow = [tableView indexPathForSelectedRow];
        }
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"filterCuisineCell";
        CuisineTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (cuisineSelected == NO) {
            cell.imageView.image = [UIImage imageNamed:@"UntickedCircle"];
        }
        cell.textLabel.text = [cuisinesArray objectAtIndex:indexPath.row];
    
        return cell;
    }
    
    1 回复  |  直到 11 年前
        1
  •  0
  •   Recycled Steel    11 年前

    因为单元格正在被重用,所以如果需要设置或取消设置,那么如果它重用单元格并且勾号在那里,那么它将再次显示。请注意,如果你有20行,但屏幕上只有4行,iOS将只绘制其中的几行,并且当其中一行被重复用于下一行时。

    大部分didSelect。。。。不需要。

    对此进行更改;

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"filterCuisineCell";
        CuisineTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (indexPath.row == selectedRow.row) {  // if you have sections then compare the whole indexPath
            cell.imageView.image = [UIImage imageNamed:@"TickedCircle"];
        }
        else
        {
            cell.imageView.image = [UIImage imageNamed:@"UntickedCircle"];
        }
        cell.textLabel.text = [cuisinesArray objectAtIndex:indexPath.row];
    
        return cell;
    }