user2396021
user2396021

Reputation: 85

Select one speicific row with one checkmark using UITbaleView

Plz anyone tell me how i select the row at one time with checkmark while other row are not checkmark.I tried but in my case there is mutiple row selected with checkmark. Basically I want to save one row with checkmark when i select another row then previous row deselect and this row select with checkmark. Here is my Code

    - (NSString *)getKeyForIndex:(int)index
    {
    return [NSString stringWithFormat:@"KEY%d",index];
    }
    - (BOOL) getCheckedForIndex:(int)index
    {
     if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]]boolValue]==YES)
     {
     return YES;
     }
     else
    {
    return NO;

       } }


- (void) checkedCellAtIndex:(int)index
    {    BOOL boolChecked = [self getCheckedForIndex:index];
        [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {
      return List.count;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {
      static NSString *subviewCells = @"Cells";

       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:subviewCells];
      if (cell == nil)
     {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:subviewCells];
      }
        cell.textLabel.text = [List objectAtIndex:indexPath.row];
          return cell;
    }

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
       UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

         [self checkedCellAtIndex:indexPath.row];

      if([self getCheckedForIndex:indexPath.row]==YES)
       {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
     }
      else
     {
        cell.accessoryType = UITableViewCellAccessoryNone;
     }
      selectLanguage = [List objectAtIndex:indexPath.row];
    }
    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
       [self checkedCellAtIndex:indexPath.row];

        if([self getCheckedForIndex:indexPath.row]==NO)
      {

        cell.accessoryType = UITableViewCellAccessoryNone;
   }
    else

      {
         cell.accessoryType = UITableViewCellAccessoryCheckmark;
     }

     selectLanguage = [List objectAtIndex:indexPath.row];
    }

    @end;

Upvotes: 0

Views: 8369

Answers (1)

Dhaval Bhadania
Dhaval Bhadania

Reputation: 3089

Try this :

Single Row Selection:

create a new variable to track the index In Controller:

 int selectedIndex;

in UITableView cellForRowAtIndexPath method:

if(indexPath.row == selectedIndex)
{
   cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
   cell.accessoryType = UITableViewCellAccessoryNone;
}

and in UITableView didSelectRowAtIndex method:

 selectedIndex = indexPath.row;
 [tableView reloadData];

2 way as per you want:

.h file:

NSIndexPath* checkedIndexPath;

@property (nonatomic, retain) NSIndexPath* checkedIndexPath;

.m file:

 @synthesize checkedIndexPath;

    - (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];
        }

         //do you stuff here
        if([self.checkedIndexPath isEqual:indexPath])
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }

        return cell;
    }


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

        //do work for checkmark
        if(self.checkedIndexPath)
        {
            UITableViewCell* uncheckCell = [tableView
                                    cellForRowAtIndexPath:self.checkedIndexPath];
            uncheckCell.accessoryType = UITableViewCellAccessoryNone;
        }
        if([self.checkedIndexPath isEqual:indexPath])
        {
            self.checkedIndexPath = nil;
        }
        else
        {
            UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            self.checkedIndexPath = indexPath;
        }
    }

Happy coding!!

Upvotes: 16

Related Questions