【问题标题】:Select and Deselect UITableView Rows选择和取消选择 UITableView 行
【发布时间】:2017-03-31 15:19:49
【问题描述】:

我有多行的 TableView,当我取消选择任何行时,我选择它们并将它们添加到标签文本中,我无法从标签文本中删除它

这是我的代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        if tableView == categoryTable
        {
        let cell = self.categoryTable.dequeueReusableCellWithIdentifier("categoryCell") as! InquiryCategoryTableViewCell

        let Category:CategoryDB = categoryData.objectAtIndex(indexPath.row) as! CategoryDB
        print("Category = \(Category.category)")

            cell.categoryName.text = "\(Category.category)"
            cell.tintColor = color.UIColorFromRGB(0xCEEBFF)
            categoryTable.allowsMultipleSelectionDuringEditing = true
            categoryTable.setEditing(true, animated: false)



        return cell
        }
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if tableView == categoryTable
        {
            categoryList = categoryData[indexPath.row] as! CategoryDB

            self.category_id = categoryList!.catg_id



            print("Name = \(categoryList!.category)")
            print("ID = \(self.category_id)")
            categoryLabel.text! += "\(categoryList!.category), "
        }
}

这是我得到的输出:

我首先选择了 3 行,它被附加到 Label.text 之后,我取消选择了行 Label.text 保持不变

谁能在这段代码中帮助我?

【问题讨论】:

  • 您想在取消选择行时更新标签?
  • @UmairAfzal 是的
  • 所以调用 didDeselectRowAtIndexPath 就像你使用 didSelectRowAtIndexPath 一样
  • @UmairAfzal 让我试试这个

标签: ios uitableview swift2 multipleselection uitableviewrowaction


【解决方案1】:

你只需要像下面这样实现UITableView的委托方法

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    // update your label here
}

【讨论】:

    【解决方案2】:

    请使用数组来执行此操作。在顶部声明 NSMutableArray

    var arrSelectedTexts:NSMutableArray = [String]()
    

    现在

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            if(!arrSelectedTexts.containsObject(categoryList!.category))
                arrSelectedTexts.addObject(categoryList!.category)
    
            //Now update your label using the objects of the array
    
    }
    
    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
            if(arrSelectedTexts.containsObject(categoryList!.category))
               arrSelectedTexts.removeObject(categoryList!.category)
    
            //Now update your label using the objects of the array
        }
    

    【讨论】:

      【解决方案3】:

      您可以在 CategoryDB 中添加一个标志 - hasSelected

      func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
      if tableView == categoryTable
          {
              categoryList = categoryData[indexPath.row] as! CategoryDB
      
              categoryList!.hasSelected = true
      
              refreshLabel()
          }
      

      }

      func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
      if tableView == categoryTable
          {
              categoryList = categoryData[indexPath.row] as! CategoryDB
      
              categoryList!.hasSelected = false
      
              refreshLabel()
          }
      

      }

      func refreshLabel(){
           var label = ""
           for object in categoryData as! CategoryDB {
              if(object.hasSelected){
                  label += "\(categoryList!.category), "
              }
           }
           categoryLabel.text! = label
      

      }

      【讨论】:

        【解决方案4】:

        首先从cellForRowAtIndexPath 中删除categoryTable.allowsMultipleSelectionDuringEditing = true 并将其添加到viewDidload,因为cellForRowAtIndexPath 很多时候您的单元格会重复使用,因此无需设置那么多次!

        现在你也应该实现didDeselectRowAtIndexPath委托方法。

        在该方法中,您将获得 indexPath.row 对应的行 deSelectunselect,从该 indexPath.row 您可以从数组中获取您需要从您所在的字符串中删除的对象显示在标签中。

        所以,在 didDeselectRowAtIndexPath 首先转换 your label text to mutable array 然后从该数组中删除对象,然后将该数组再次转换为字符串并显示在标签上!

        【讨论】:

          【解决方案5】:

          试试这个,它非常适合我,

          考虑这个示例变量

          let animals : [String] = ["Dog","Cat","Lion","Tiger","Cow","Buffalo","Horse"]
          var sampleArray = [String]()
          var samleLabel = UILabel()
          

          在您的 didSelectRowAtIndexPath 中添加此功能

          func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
          { 
              let cell = tableView.cellForRowAtIndexPath(indexPath)!
              cell.textLabel!.text = animals [indexPath.row]
          
              if sampleArray.contains((cell.textLabel?.text)!)
              {
                  sampleArray.removeAtIndex(indexPath.row)
                  print(sampleArray)
                  samleLabel.text = test().componentsJoinedByString(",")
              }
              else
              {
                  sampleArray.append((cell.textLabel?.text)!)
                   print(sampleArray)
                  samleLabel.text = test().componentsJoinedByString(",")
              }  
          }
          
          func test()-> NSArray
          {
              print(sampleArray)
              return sampleArray;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-03-17
            • 1970-01-01
            • 1970-01-01
            • 2015-05-19
            • 2012-06-27
            • 1970-01-01
            相关资源
            最近更新 更多