首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌入UIContainerView信元重用的UIContainerView

嵌入UIContainerView信元重用的UIContainerView
EN

Stack Overflow用户
提问于 2014-03-12 01:38:43
回答 1查看 187关注 0票数 0

我有一个启用滚动功能的UITableViewController,嵌入到UIContainerView中。在tableview中有一个自定义的UITableViewCell,其中包含一个UIImageView,当您在表视图中选择一个多个单元格时,我将获得索引路径并设置所选单元格的图像视图以显示一个滴答。表视图由来自数组的多个数据块动态填充。很简单。

问题是,由于tableview比容器视图中可见的要长,所以索引路径似乎只应用于可见的内容。这意味着它使用相应的索引路径将单元格设置为带有勾号的可见索引路径。

假设五个单元格可见,第四个单元格被选中。你再往下走五个单元格,在第十个单元格上有一个滴答,但从技术上讲,它是不可见内容的第五个单元格。我想这与细胞再利用有关,但我不知道该怎么做。

怎么解决这个问题呢?(代码如下)

代码语言:javascript
复制
    - (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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-12 10:29:23

因为单元格被重用,如果需要设置或取消设置的话,如果它重用了单元格,并且滴答在那里,那么它将再次显示。请注意,如果您有20行,但屏幕上只有4行,iOS将只绘制其中的几行,并在下一次开始时重用其中的一个卷轴。

你的大部分didSelect..。不需要。

改变到这一点;

代码语言:javascript
复制
- (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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22340288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档