Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ios-如何在一次单击中选择多个单元格(Objective-c)_Ios_Objective C_Uitableview - Fatal编程技术网

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

ios-如何在一次单击中选择多个单元格(Objective-c),ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在编写一个iOS应用程序,想知道当用户在uitableview中单击单个单元格时,是否可以同时选择3个单元格。所以他们点击的是3,下面的2。这可能吗?如果可能,将如何实现?谢谢 我当前如何检查用户单击: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self tableView:tableView canCollapseSection

我正在编写一个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];
                  });

            }
        }
}

在我的代码中,问题是它不允许我一次选择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];
}
//选择节0中的所有行
对于(int i=0;i<[表视图行数部分:0];i++){
nsindepath*iPath=[nsindepath indexPathForRow:i第1节:0];
[tableView selectRowAtIndexPath:iPath动画:无滚动位置:UITableView滚动位置无];
}
//选择所有可见行
对于(在[tableView indexPathsForVisibleRows]中的NSIndexPath*iPath){
[tableView selectRowAtIndexPath:iPath动画:无滚动位置:UITableView滚动位置无];
}
//选择第2、4和6行
对于(NSNumber*n in@[@2,4,6]){
nsindepath*iPath=[nsindepath indexPathForRow:[n integerValue]instation:0];
[tableView selectRowAtIndexPath:iPath动画:无滚动位置:UITableView滚动位置无];
}
//取消选择所有行
对于(NSIndexPath*iPath在[tableView indexPathsForSelectedRows]中){
[tableView取消行索引路径:iPath动画:否];
}

Swift

// 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)
}
//选择节0中的所有行

对于0中的i..来回答您关于一次单击选择多个单元格的问题(我暂时忽略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];
}
//选择节0中的所有行
对于(int i=0;i<[表视图行数部分:0];i++){
nsindepath*iPath=[nsindepath indexPathForRow:i第1节:0];
[tableView selectRowAtIndexPath:iPath动画:无滚动位置:UITableView滚动位置无];
}
//选择所有可见行
对于(在[tableView indexPathsForVisibleRows]中的NSIndexPath*iPath){
[tableView selectRowAtIndexPath:iPath动画:无滚动位置:UITableView滚动位置无];
}
//选择第2、4和6行
对于(NSNumber*n in@[@2,4,6]){
nsindepath*iPath=[nsindepath indexPathForRow:[n integerValue]instation:0];
[tableView selectRowAtIndexPath:iPath动画:无滚动位置:UITableView滚动位置无];
}
//取消选择所有行
对于(NSIndexPath*iPath在[tableView indexPathsForSelectedRows]中){
[tableView取消行索引路径:iPath动画:否];
}

Swift

// 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)
}
//选择节0中的所有行

对于0中的i..它与objective-c类似吗?另外,如何检查当前选择的行?有没有一个简单的方法可以做到这一点?@apex-我建议阅读文档。。。(尽管我在示例中包括了这一点:
[tableView indexPathsForSelectedRows]
)它在objective-c中类似吗?另外,我如何检查当前选择了哪些行?有没有一个简单的方法可以做到这一点?@apex-我建议阅读文档。。。(尽管我在示例中包括了这一点:
[tableView indexPathsForSelectedRows]