swift tableview how to select all rows

前端 未结 3 1770
刺人心
刺人心 2021-01-26 19:12

I have Button in tableview I want when I press that button will select all cell rows, how to do that? I tried alot but I got nothing

I\'m so confused how to make the but

相关标签:
3条回答
  • 2021-01-26 19:54
    var selectedUsernameArray : NSMutableArray = []
    var username1Array : NSMutableArray = ["hel","dsf","fsdg"]//just a sample
    

    Initially button name should be "Select all"

    Button action :

     @IBAction func buttonAction(sender: UIButton) {
    
       if(sender.titleLabel?.text == "Select all")
        {
            selectedUsernameArray .addObjectsFromArray(username1Array as [AnyObject])
            sender.titleLabel?.text = "Deselect all"
        }
        else
       {
        selectedUsernameArray .removeAllObjects();
        sender.titleLabel?.text = "Select all"
        }
    
        self.tableView .reloadData()
    }
    

    tableview delegate

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
        var ob: AnyObject = username1Array .objectAtIndex(indexPath.row);
        cell.textLabel?.text = ob as? String;
        if(selectedUsernameArray .containsObject(ob))
        {
          cell.backgroundColor = UIColor.blueColor();
        }
        else
        {
            cell.backgroundColor = UIColor.whiteColor();
        }
        return cell
    }
    
    0 讨论(0)
  • 2021-01-26 20:01

    In cellForRowAtIndexPath:

    UITableViewCell *cell...
    
    cell.accesoryType = isSelectAll ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    
    0 讨论(0)
  • 2021-01-26 20:09
    var isSelectAll = false; //global variable
    
    isSelectAll = true;//on button action
    self.tableView.reloadData()//on button action
    

    in cellForRowAtIndexPath method

    if(isSelectAll==true)
    {
    cell.backgroundColor = UIColor.blueColor();
    }
    else
    {
    cell.backgroundColor = UIColor.white();
    }
    
    0 讨论(0)
提交回复
热议问题