abhiTouchmagic
abhiTouchmagic

Reputation: 323

how to code to select multiple row with check mark?

I have taken UITableView. I want to use checkmark on row. When row is selected check mark should show there and when he selects check mark row then check mark should disappear. I used following code.

In my code only I select multiple row but am able to disappear on same checkmark with selected row.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //if (cell == nil) {
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    cell.textLabel.text=[[gmailList objectAtIndex:indexPath.row]valueForKey:@"Name"];

    if(cell.selected)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    // Configure the cell...

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Uncheck the previous checked row
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];

    if(cell.selected)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;

        cell.selected=FALSE;

    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
              cell.selected=TRUE;
    }


}

Upvotes: 1

Views: 2228

Answers (3)

Pengsreang Srun
Pengsreang Srun

Reputation: 46

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath{
    if ([tableView tag]==2){
    self.clearCheckMark = false;
    if ([tableView cellForRowAtIndexPath:indexPath].accessoryType  ==UITableViewCellAccessoryCheckmark)
   {
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
        }
        else
        {
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
        }
        self.index = [tableView indexPathsForSelectedRows];

        }
      return indexPath;}

Upvotes: 3

Pawan Jat
Pawan Jat

Reputation: 81

Try this- You have to define a global NSMutableDict and use that dict as below

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //if (cell == nil) {
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    cell.textLabel.text=[[gmailList objectAtIndex:indexPath.row]valueForKey:@"Name"];
    if([[dict objectForKey:[NSString stringWithFormat:"%d",indexPath.row]] isEqualToString:@"YES"])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    // Configure the cell...
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Uncheck the previous checked row
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    if(cell.selected)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [[dict setObject:@"NO"]] forKey:[NSString stringWithFormat:"%d",indexPath.row];
        cell.selected=FALSE;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
              cell.selected=TRUE;
        [[dict setObject:@"YES"]] forKey:[NSString stringWithFormat:"%d",indexPath.row];
    }
}

Upvotes: 3

krunal
krunal

Reputation: 482

Set NSMutableDictionary with key like "isChecked" "YES" or "NO" using below method

- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
NSMutableDictionary *dic = [gmailList objectAtIndex:indexPath.row];

if([[dic valueForKey:@"isChecked"]boolValue])
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [dic setObject:@"NO" forKey:@"isChecked"];
        cell.selected=FALSE;

    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [dic setObject:@"YES" forKey:@"isChecked"];
        cell.selected=TRUE;
    }
   [self.tableView reloadData];
}

Upvotes: 0

Related Questions