【问题标题】:UITableView Embedded In UIContainerView Cell ReusageUITableView 嵌入 UIContainerView 单元格重用
【发布时间】:2014-04-15 22:11:17
【问题描述】:

我有一个启用滚动的 UITableViewController,嵌入在 UIContainerView 中。在 tableview 中有一个自定义 UITableViewCell ,其中有一个 UIImageView ,当您在 tableview 中选择多个单元格之一时,我获取索引路径并将所选单元格的图像视图设置为显示一个刻度。表格视图由数组中的多条数据动态填充。很简单。

问题是,由于 tableview 比在容器视图中可见的要长,似乎索引路径仅适用于可见内容。这意味着它将具有相应索引路径的单元格设置为带有刻度的可见单元格。

假设五个单元格可见,则选择第四个。你再往下走五个单元格,并且在应该是第十个单元格的地方打了一个勾,但从技术上讲,它是不可见内容的第五个单元格。我认为这与细胞重复使用有关,但我不确定该怎么做。

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

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

【问题讨论】:

  • 你能展示你的cellForRowAtindexPath函数吗?
  • 您好,刚刚添加到说明中。

标签: objective-c uitableview nsindexpath uicontainerview


【解决方案1】:

因为单元格正在被重用,如果需要设置或取消设置的所有部分,如果它重用单元格并且打勾在那里,那么它将再次显示。请注意,如果您有 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多