0

I have the following code in my didSelectRowAtIndexPath: method, and I want the user to be able to tap on a cell, have the cell show the checkmark, and add the selected cells to a separate array. I also would like the user to be able to tap an already selected cell, which will then take away the checkmark and remove it from the array.

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.setupTable deselectRowAtIndexPath:indexPath animated:YES];

    SetupCell *cell = (SetupCell*)[tableView cellForRowAtIndexPath:indexPath];

    NSLog(@"Feed name: %@", cell.setupFeedName.text);

    if ([self.selectedCells containsObject:cell.setupFeedName.text]) {
        [self.selectedCells removeObjectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        [self.selectedCells addObject:cell.setupFeedName.text];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    [self.setupTable reloadData];

    NSLog(@"%i cells are selected.", [self.selectedCells count]);
    NSLog(@"%i cells are not selected.", [self.setupFeeds count]);

}

The NSLog outputs the following:

2013-08-12 07:04:58.767 [13921:c07] Feed name: Politico
2013-08-12 07:04:58.768 [13921:c07] 0 cells are selected.
2013-08-12 07:04:58.769 [13921:c07] 6 cells are not selected.

UPDATE:

Here is what Console shows:

2013-08-12 07:31:40.390[14117:c07] Feed name: Huffington Post
2013-08-12 07:31:40.392[14117:c07] 5 cells are selected.
2013-08-12 07:31:40.392[14117:c07] 6 cells are not selected.

Here is the code:

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.setupTable deselectRowAtIndexPath:indexPath animated:YES];

    SetupCell *cell = (SetupCell*)[tableView cellForRowAtIndexPath:indexPath];

    NSLog(@"Feed name: %@", cell.setupFeedName.text);

    if ([[self.selectedCells objectAtIndex:indexPath.row] isEqualToString:cell.setupFeedName.text]) {
        [self.selectedCells removeObjectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        [self.selectedCells addObject:cell.setupFeedName.text];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    NSLog(@"%i cells are selected.", [self.selectedCells count]);
    NSLog(@"%i cells are not selected.", [self.setupFeeds count]);

    [self.setupTable reloadData];

}

3 Answers3

0

I did that in way below, but i took the custom buttons on cell to check uncheck.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

 UITableViewCell * cell = 
 [tableHistory dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

 if (cell == nil) {

 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle    reuseIdentifier:SimpleTableIdentifier] autorelease];
 }

 else
 {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier] autorelease];
 }

 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)];

if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"check"])

[button setImage:[UIImage imageNamed:@"right-with-bg.png"] forState:UIControlStateNormal];
 else
[button setImage:[UIImage imageNamed:@"tick-bg.png"] forState:UIControlStateNormal];

 [button addTarget:self   action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];

 [cell.contentView addSubview:button];


 cell.textLabel.text = [cellDataArray objectAtIndex:indexPath.row];

 cell.detailTextLabel.text = [arrayTktcode objectAtIndex:indexPath.row];

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

[cell addSubview:btnUnScan];

[cell addSubview:btnUScan];

return cell;

}

-(void)buttonClicked:(id)sender
{

CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tableHistory];
NSIndexPath *indexPath = [tableHistory indexPathForRowAtPoint:touchPoint];

UIButton *button = (UIButton *)sender;

if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
{
[button setImage:[UIImage imageNamed:@"right-with-bg.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"check"];
 }
 else
{
[button setImage:[UIImage imageNamed:@"tick-bg.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
}
}

check this also.

Community
  • 1
  • 1
iPhone Programmatically
  • 1,211
  • 2
  • 22
  • 54
0

You need to create an array which stores the selected cells. And keep the track of selected cells. If cell is tapped again then remove that particular cell from array.

Take detailed information from example given

Niru Mukund Shah
  • 4,637
  • 2
  • 20
  • 34
0
   // use this in didSelectRow
    NSUInteger index = [self.selectedCells indexOfObject:cell.setupFeedName.text];
    if (index != NSNotFound) {
       [self.selectedCells removeObjectAtIndex:indexPath.row];
            cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else

    {
     [self.selectedCells addObject:cell.setupFeedName.text];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
h.kishan
  • 681
  • 6
  • 20