繁体   English   中英

ios-如何一次单击即可选择多个单元格(Objective-c)

[英]ios - How to select multiple cells on a single click (Objective-c)

我正在编写一个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。

要回答有关单击一次选择多个单元格的问题(我暂时忽略您的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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM