ios - 如何通过单击选择多个单元格(Objective-c)

时间:2017-07-05 19:43:47

标签: ios objective-c uitableview

我正在编写一个iOS应用程序,并且想知道当用户点击uitableview中的单个单元格时是否可以一次选择3个单元格。所以3是他们点击的那个和下面的2。这是可能的,如果是这样,它将如何做?感谢

我目前如何检查用户点击次数:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self tableView:tableView canCollapseSection:indexPath.section])
    {
        if (!indexPath.row)
        {
            // only first row toggles exapand/collapse
            [tableView deselectRowAtIndexPath:indexPath animated:YES];

            NSInteger section = indexPath.section;
            BOOL currentlyExpanded = [expandedSections containsIndex:section];
            NSInteger rows;

            NSMutableArray *tmpArray = [NSMutableArray array];


            if (currentlyExpanded)
            {
                rows = [self tableView:tableView numberOfRowsInSection:section];
                [expandedSections removeIndex:section];

            }
            else
            {

                [expandedSections addIndex:section];
                rows = [self tableView:tableView numberOfRowsInSection:section];
            }

            for (int i=1; i<rows; i++)
            {
                NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
                                                               inSection:section];
                [tmpArray addObject:tmpIndexPath];
            }

            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

            if (currentlyExpanded)
            {
                dispatch_async(dispatch_get_main_queue(), ^{

                [tableView deleteRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationTop];
                });
            }
            else
            {
                  dispatch_async(dispatch_get_main_queue(), ^{
                [tableView insertRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationTop];
                  });

            }
        }
}

我也有

self.tableView.allowsMultipleSelection = true;

在我的代码中,但问题是它不允许我一次选择3个。

1 个答案:

答案 0 :(得分:0)

回答关于一键选择多个单元格的问题(我暂时忽略了insertRows / deleteRows的内容)...

以下是选择多行的几个示例。您应该毫不费力地为您的具体实现这一点:

<强>目标C

// select ALL rows in Section 0
for (int i = 0; i < [tableView numberOfRowsInSection:0]; i++) {
    NSIndexPath *iPath = [NSIndexPath indexPathForRow:i inSection:0];
    [tableView selectRowAtIndexPath:iPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

// select ALL VISIBLE rows
for (NSIndexPath *iPath in [tableView indexPathsForVisibleRows]) {
    [tableView selectRowAtIndexPath:iPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

// select rows 2, 4 and 6
for (NSNumber *n in @[@2, @4, @6]) {
    NSIndexPath *iPath = [NSIndexPath indexPathForRow:[n integerValue] inSection:0];
    [tableView selectRowAtIndexPath:iPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

// deselect all rows
for (NSIndexPath *iPath in [tableView indexPathsForSelectedRows]) {
    [tableView deselectRowAtIndexPath:iPath animated:NO];
}

<强>夫特

// select ALL rows in Section 0
for i in 0..<tableView.numberOfRows(inSection: 0) {
    let iPath = IndexPath(item: i, section: 0)
    tableView.selectRow(at: iPath, animated: false, scrollPosition: .none)
}

// select ALL VISIBLE rows
for iPath in tableView.indexPathsForVisibleRows! {
    tableView.selectRow(at: iPath, animated: false, scrollPosition: .none)
}

// select rows 2, 4 and 6
for i in [2, 4, 6] {
    let iPath = IndexPath(item: i, section: 0)
    tableView.selectRow(at: iPath, animated: false, scrollPosition: .none)
}

// deselect all rows
for iPath in tableView.indexPathsForSelectedRows! {
    tableView.deselectRow(at: iPath, animated: false)
}