1

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 button contact the cell

I've tried to make var like this

var x = false

then I do like

if( x == true ) { //Code }

and when you press the button it will be true

but I don't know in which func I should put this ? and I don't know how to select all rows

Could someone help me how to Select \ Deselect all rows in cell when pressing button in tableview.

Thank you so much!

ase
  • 13,231
  • 4
  • 34
  • 46
Salah
  • 933
  • 3
  • 13
  • 32
  • if there's no func to select all, is there a way to change all rows background color when i press the button ? – Salah Aug 17 '15 at 08:21
  • Background color of entire view? – Shebin Koshy Aug 17 '15 at 08:27
  • background for cell rows, the cell using other swift file and table view using another swift file the button in the tableview – Salah Aug 17 '15 at 08:29
  • on button action set a bool value , reload the tableView . so cellForRowAtIndexPath method will invoke, in this method u check condition with bool value and set cell.backgroundColor = UIColor.blueColor(); – Shebin Koshy Aug 17 '15 at 08:32
  • http://stackoverflow.com/questions/2035061/select-tableview-row-programmatically – Adnan Aftab Aug 17 '15 at 09:13

3 Answers3

5
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
}
Shebin Koshy
  • 1,182
  • 8
  • 22
1
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();
}
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Shebin Koshy
  • 1,182
  • 8
  • 22
0

In cellForRowAtIndexPath:

UITableViewCell *cell...

cell.accesoryType = isSelectAll ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
Vito Royeca
  • 657
  • 10
  • 20