ios - 如何知道 UITableViewCell 是否未显示在 UITableView 上

标签 ios objective-c iphone uitableview

我开发了一个 UITableView,其中包含一些可以包含子单元的单元格。

例子:

enter image description here

我的应用程序允许用户移动单元格位置,但是,当一个表格有一个或多个带有子单元格的单元格时,我会折叠所有这些单元格,以便只允许移动具有相同标识(或同一级别)的单元格

如果表格很长,当我折叠带有子元素的单元格时,我使用该单元格的 indexPath 从表格中删除子元素。我的问题是其中一些单元格不在内存中,所以我遇到了崩溃。

如何检测未显示单元格以便仅从数据源中删除元素?

这是我折叠/展开元素的方法:

- (void)expandCollapseSubtasks:(UIButton *)sender
{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:_taskTableView]; //when CGPoint = 0,0 not remove cells?
    NSIndexPath *indexPath = [_taskTableView indexPathForRowAtPoint:buttonPosition];

    if (indexPath != nil)
    {
        Task *task = [_tasks objectAtIndex:indexPath.row];
        TaskTitleCell *cell = (TaskTitleCell *)[_taskTableView cellForRowAtIndexPath:indexPath];
        UILabel *subtasksNumberLabel = (UILabel *)[cell viewWithTag:107];
        UIButton *subtasksButton = (UIButton *)[cell viewWithTag:108];

        NSMutableArray *subtasksIndexPaths = [[NSMutableArray alloc] init];

        NSNumber *hidden;
        //Expand
        if (!subtasksButton.selected){
            hidden = @0;
            subtasksNumberLabel.textColor = [UIColor colorWithRed:72.0/255.0 green:175.0/255.0 blue:237.0/255.0 alpha:1.0];

            NSDictionary *subtasksAndIndexesDictionary = [task getSubtasksIndexesInTaskCollection:_tasks ofList:task.list];

            NSIndexSet *indexes = [subtasksAndIndexesDictionary objectForKey:@"indexes"];
            NSArray *subtasks = [subtasksAndIndexesDictionary objectForKey:@"subtasks"];

            [_tasks insertObjects:subtasks atIndexes:indexes];

            [indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
                NSIndexPath *subtaskIndexPath = [NSIndexPath indexPathForRow:idx inSection:0];
                [subtasksIndexPaths addObject:subtaskIndexPath];
            }];

            [_taskTableView insertRowsAtIndexPaths:subtasksIndexPaths withRowAnimation:UITableViewRowAnimationTop];

        //Collapse
        }else{
            hidden = @1;
            subtasksNumberLabel.textColor = [UIColor whiteColor];

            NSArray *subtasks = [task getSubtasks];

            [_tasks removeObjectsInArray:subtasks];

            for (int i=1;i<=subtasks.count; i++){
                NSIndexPath *subtaskIndexPath = [NSIndexPath indexPathForRow:indexPath.row+i inSection:0];
                [subtasksIndexPaths addObject:subtaskIndexPath];
            }

            [_taskTableView deleteRowsAtIndexPaths:subtasksIndexPaths withRowAnimation:UITableViewRowAnimationBottom];

        }

        subtasksButton.selected = !subtasksButton.selected;
        task.hidesubtasks = hidden;

        [[NSManagedObjectContext defaultContext] saveToPersistentStoreAndWait];
    }
}

和电话

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
UITableViewCell *cell = [_taskTableView cellForRowAtIndexPath:indexPath];
UIButton *subtasksButton = (UIButton *)[cell viewWithTag:108];
[self expandCollapseSubtasks:subtasksButton];

其中idx是元素在dataSource中的位置

最佳答案

您可以获得所有可见单元格的 NSIndexPath 数组:

NSArray *array = [self.tableView indexPathsForVisibleRows];

您还可以获取单元格数组本身而不是 NSIndexPath:

[tableView visibleCells]

这就是您找出不可见细胞所需要的。

关于ios - 如何知道 UITableViewCell 是否未显示在 UITableView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32460511/

相关文章:

ios - 尝试安装新的 Google Analytics 3.0 Beta 时出现链接器错误

ios - 从 didFinishLaunchingWithOptions 终止应用程序

iphone - 是否可以在没有按钮的情况下切换 View ?

iphone - 使用字典数组中的数据填充 Tapku 日历

iphone - 语义问题 : Why does this warning appear here

ios - 无权读取 iCloud Drive 文件

ios - 传递字符串时崩溃

ios - 轻量级迁移完成通知

ios - CLLocationManager 委托(delegate)方法 didUpdateToLocation 未在用户设备上调用

iphone - 在ios中通过解析xml获取元素的值