我开发了一个UITableView,其中有些单元格可能具有子单元格。

例:

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

我的应用程序允许用户移动单元格的位置,但是,当一个表中有一个或多个带有子单元格的单元格时,我将它们全部折叠起来,以仅允许移动具有相同标识(或相同级别)的单元格

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

我如何才能检测到未显示某个单元格,以便仅从dataSource中删除该元素?

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

- (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是元素在数据源中的位置

最佳答案

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

NSArray *array = [self.tableView indexPathsForVisibleRows];


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

[tableView visibleCells]


那就是您需要找出不可见的单元格。

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

10-08 21:14