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

时间:2015-09-08 14:33:30

标签: ios objective-c iphone uitableview

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

示例:

enter image description here

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

如果表格很长,当我使用子元素折叠单元格时,我使用该单元格的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是dataSource中元素的位置

1 个答案:

答案 0 :(得分:1)

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

NSArray *array = [self.tableView indexPathsForVisibleRows];

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

[tableView visibleCells]

你需要找出看不见的细胞。