Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed to create a select all button for UITableView and add selections to an array in iOS
    primarykey
    data
    text
    <p>I have a UITableViewController that shows a list of items from NSMutableArray, along with a button that serves as a check box that the user can select/deselect. I am able to successfully display the table, as well as the checkboxes. However, I would like to have a tableheader at the very top of the table that would have a "select all" check box, which would allow the user to select all of the cells, or deselect all of the cells. If some cells are already checked off, then I want only those cells that are not selected, to be selected. </p> <p>Here is the code that I have thus far:</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleNone; testButton = [UIButton buttonWithType:UIButtonTypeCustom]; [testButton setFrame:CGRectMake(280, 57, 25, 25)]; [testButton setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateSelected]; [testButton setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal]; [testButton setUserInteractionEnabled:YES]; [testButton addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside]; [cell setAccessoryView:testButton]; } // Configure the cell... TestObject *tObject = [[DataModel sharedInstance].testList objectAtIndex:indexPath.row]; cell.textLabel.text = tObject.testTitle; return cell; } -(void)buttonTouched:(id)sender { UIButton *btn = (UIButton *)sender; if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"CheckBox1.png"]]) { [btn setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal]; // other statements } else { [btn setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateNormal]; // other statements } } //Here is the additional method that I have added to my code to select all -(void)clickOnCheckButton { NSLog(@"Did it select?"); for (int i = 0; i &lt; [self.tableView numberOfSections]; i++) { for (int j = 0; j &lt; [self.tableView numberOfRowsInSection:i]; j++) { NSUInteger ints[2] = {i,j}; NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2]; UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; //Here is your code [self buttonTouched:nil]; } } } </code></pre> <p>Does anyone have any suggestions on how to do this?</p> <p>Thanks in advance to all who reply.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload