【问题标题】:How to check the selected row in UITableView is of which group/section如何检查 UITableView 中的选定行属于哪个组/部分
【发布时间】:2016-04-14 10:55:32
【问题描述】:

是否可以在 UITableView 中获取相应选定行的节数

【问题讨论】:

标签: ios uitableview


【解决方案1】:

是的,您可以使用

检查部分
indexPath.section

【讨论】:

    【解决方案2】:

    如果你有 NSIndexPath,你可以得到 row by

    indexPath.row 
    

    或部分

    indexPath.section
    

    【讨论】:

      【解决方案3】:

      试试下面的示例代码:

      #pragma mark - Table view data source
      
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
          return 5;
      }
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
          NSInteger rows = 0;
      
          switch (section) {
              case 0:
                  rows = 7;
                  break;
              case 1:
                  rows = 21;
                  break;
      
              case 2:
                  rows = 3;
                  break;
      
              case 3:
                  rows = 1;
                  break;
      
              case 4:
                  rows = 5;
                  break;
      
              default:
                  break;
          }
          return rows;
      }
      
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
      
          // Configure the cell...
      
          cell.textLabel.text = [NSString stringWithFormat:@"section : %ld & rows : %ld",(long)indexPath.section,(long)indexPath.row];
          return cell;
      }
      
      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
          //This will print which row of which section is clicked
          NSLog(@"section : %ld & rows : %ld",(long)indexPath.section,(long)indexPath.row);
      
      }
      

      【讨论】:

        【解决方案4】:

        这是对的!

           NSArray <NSIndexPath *>*rows = [tableView indexPathsForSelectedRows];
        

        【讨论】:

          【解决方案5】:

          我解决了我正在寻找的问题。我想在单击按钮时获取其中各个选定行的所有部分编号。

          我用过 NSArray *paths = [self.tableView indexPathsForSelectedRows];

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-02-03
            • 1970-01-01
            • 2016-07-18
            • 2013-09-04
            相关资源
            最近更新 更多