Reputation: 149
Till now i didn't got solution for this,
Two process,
i have one button in tableview, if i click the button, display all checkmarks in that section, otherwise none (toggling function).
each and every cell is clicked display checkmark, otherwise none.
i have done first case but i want second also.
My code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = nil;
NSString *key = [_keys objectAtIndex:indexPath.section];
NSArray *name = [_names objectForKey:key];
static NSString *MyCellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyCellIdentifier];
cell.textLabel.text = [name objectAtIndex:indexPath.row];
if([selectAll.currentTitle isEqualToString:@"Deselect All"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
selectAll.frame = CGRectMake(frame.size.width-90, 5, 86, 30);
}
else {
selectAll.frame = CGRectMake(cmdSwitch.frame.origin.x, 0, 90, 30);
}
if([[NSUserDefaults standardUserDefaults] valueForKey:@"Sel_Check"]==nil)
{
[selectAll setTitle:@"Deselect All" forState:UIControlStateNormal];
}
else
{
if([[[NSUserDefaults standardUserDefaults] valueForKey:@"Sel_Check"] isEqualToString:@"1"])
{
[selectAll setTitle:@"Deselect All" forState:UIControlStateNormal];
}
else if([[[NSUserDefaults standardUserDefaults] valueForKey:@"Sel_Check"] isEqualToString:@"0"])
{
[selectAll setTitle:@"Select All" forState:UIControlStateNormal];
}
}
selectAll.layer.cornerRadius = 7;
selectAll.clipsToBounds = YES;
selectAll.titleLabel.font = [UIFont boldSystemFontOfSize:15];
selectAll.backgroundColor = [UIColor whiteColor];
selectAll.layer.borderColor = [UIColor colorWithRed:155.0f/255.0f green:155.0f/255.0f blue:155.0f/255.0f alpha:1.0f].CGColor;
selectAll.layer.borderWidth = 1;
[selectAll setTitleColor:[UIColor colorWithRed:43.0f/255.0f green:65.0f/255.0f blue:116.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
[selectAll setExclusiveTouch:YES];
[selectAll addTarget:self action:@selector(mySelect:) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:selectAll];
}
return headerView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self checkedCellAtIndex:indexPath.row];
if([self getCheckedForIndex:indexPath.row]==YES)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
// my button function
- (void) mySelect:(NSIndexPath *)indexPath {
if([selectAll.currentTitle isEqualToString:@"Deselect All"])
{
[selectAll setTitle:@"Select All" forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setValue:@"0" forKey:@"Sel_Check"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
[selectAll setTitle:@"Deselect All" forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"Sel_Check"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[self.tableView reloadData];
}
like this,
Upvotes: 1
Views: 577
Reputation: 4653
Steps to follow:
(1) Make an array (i.e arrCheckedItems) for holding checked cell texts (i.e. @"Five Cents" , @"Ten Cents").
(2) When user select any cell, add that cell's text to arrCheckedItems and when deselect remove that text from array. Then reload the table.
(3) in cellForRowAtIndexPath: method, check cell's text-
if([arrCheckedItems containsObject: cell.text])
{
// cell.text = @"Five Cents" or @"Ten Cents"
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
Upvotes: 1
Reputation: 80
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = nil;
static NSString *MyCellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyCellIdentifier];
cell.textLabel.text = [candidates objectAtIndex:indexPath.row];
if([self.selectButton.titleLabel.text isEqualToString:@"Deselect All"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
return cell;
}
Upvotes: 1
Reputation: 7343
You should rethink the solution to no. 1 as well. You should have an NSMutableSet
containing the NSIndexPath
's of the selected cells.
When you tap only one cell, add that index path to the set or remove it (if it is already there) then reload the selected row.
When you tap the button above the table, you either add all the existing index paths to the set or empty the set. And then of course call [self.tableView reloadData]
.
In your tableView:cellForRowAtIndexPath:
the condition would change to this:
if([_indexPathsOfSelectedRows containsObject:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
Upvotes: 2
Reputation: 5602
If you need to select or Deselect Cell on tap than :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
Upvotes: 0