【问题标题】:How to select multiple rows in UITableView in edit mode?如何在编辑模式下在 UITableView 中选择多行?
【发布时间】:2016-03-02 10:45:24
【问题描述】:

我想问,我如何转发到编辑模式,在那里我可以选择多行,就像在消息应用程序中一样,当你点击右上角的“选择”按钮时,你可以通过点击圆圈来选择多条消息.

像这样:

我搜索了很多,真的,但找不到任何东西。谁能帮我?一些建议

【问题讨论】:

    标签: ios swift uitableview multipleselection


    【解决方案1】:

    设置

    tableView.allowsMultipleSelectionDuringEditing = true
    

    截图

    演示代码

    class TableviewController:UITableViewController{
        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.allowsMultipleSelectionDuringEditing = true
            tableView.setEditing(true, animated: false)
        }
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
            cell.textLabel?.text = "\(indexPath.row)"
            return cell
        }
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 10
        }
    }
    

    【讨论】:

    • 如何让前两个单元格默认选中?我试过“cell.setSelected(true, animated: false)”但没有用。
    • 这对我有用 "tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)"
    • 如何获取选中的单元格?
    • 你能告诉我为什么它不适用于自定义 UITableviewCell 吗?
    • 请有人指导我如何在编辑过程中实现全选和取消全选
    【解决方案2】:

    如果您使用情节提要,请执行以下操作:

    【讨论】:

      【解决方案3】:
      NSMutableArray *selected;
      

      在你的 viewcontroller.h 文件中清除它..

      selected =[[NSMutableArray alloc]init];
      for (int i=0; i<[YOUR_ARRAY count]; i++) // Number of Rows count
              {
                  [selected addObject:@"NO"];
              }
      

      使用上述代码在所选数组中添加相同数量的“NO”,因为您必须将 YOUR_ARRAY 替换为您在表中显示的数据数组。

          if(![[selected objectAtIndex:indexPath.row] isEqualToString:@"NO"])
      {
      
          cell.accessoryType=UITableViewCellAccessoryCheckmark;
      
      }
      
      else
      {
          cell.accessoryType=UITableViewCellAccessoryNone;
      }
      

      把上面的代码放在你的 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

      -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
      
      
      if (cell.accessoryType == UITableViewCellAccessoryNone) {
          cell.accessoryType = UITableViewCellAccessoryCheckmark;
          [selected replaceObjectAtIndex:path.row withObject:@"YES"];
      } else {
          cell.accessoryType = UITableViewCellAccessoryNone;
          [selected replaceObjectAtIndex:path.row withObject:@"NO"];
      }
      

      }

      也让它正常工作..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-19
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 2014-03-03
        • 2011-08-02
        相关资源
        最近更新 更多