Udai kumar Udai kumar - 3 months ago 5
Objective-C Question

How to show selected checked TableViewCell in IOS(Objective c)

Hi friends I am new in IOS. I am creating a table view of state list. On button click I'm showing a TableView on popup. Code for creating state list is

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
selectedState = [NSMutableArray new];
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth-50, screenHeight-50)];
mytableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth-50, screenHeight-100)];
mytableview.delegate =self;
mytableview.dataSource=self;
[contentView addSubview:mytableview];
UIButton *save = [[UIButton alloc] initWithFrame:CGRectMake(0, screenHeight-100, screenWidth-50, 50)];
[save setTitle:@"SAVE" forState:UIControlStateNormal];
save.titleLabel.font = [UIFont systemFontOfSize:14];
[save setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[save addTarget:self action:@selector(saveActionForState:) forControlEvents:UIControlEventTouchUpInside];
[save setBackgroundColor:[UIColor colorWithRed:41.0/255.0 green:178.0/255.0 blue:165.0/255.0 alpha:1.0]];
[contentView addSubview:save];

[[KGModal sharedInstance] showWithContentView:contentView andAnimated:YES];


For table creation code is

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [stateList count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CRTableViewCellIdentifier = @"cellIdentifier";
if(selectStateButton ==1){

CRTableViewCell *cell = (CRTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CRTableViewCellIdentifier];

if (cell == nil) {
cell = [[CRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CRTableViewCellIdentifier];
}


NSString *text = [stateList objectAtIndex:[indexPath row]];
cell.textLabel.font = [UIFont systemFontOfSize:12.0];
cell.isSelected = [selectedState containsObject:text] ? YES : NO;
cell.textLabel.text = text;




return cell;
}
}


On selection of table cell checked the cell of IOS.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectStateButton ==1){
//[selectedStatesForSelect2 removeAllObjects];
NSString *text = [stateList objectAtIndex:[indexPath row]];
NSInteger indexings = [indexPath row]+1;

if ([selectedState containsObject:text]){
[selectedState removeObject:text];
[selectedStatesId removeObject:[NSString stringWithFormat:@"%ld", (long)indexings]];
}else{
//NSLog(@"count%lu indexing %@",(unsigned long)[selectedState count],selectedStatesId);
[selectedState addObject:text];
[selectedStatesId addObject:[NSString stringWithFormat:@"%ld", (long)indexings]];
[selectedStatesForSelect2 addObject:[NSString stringWithFormat:@"%ld", (long)indexings]];
NSLog(@"select%@count%lu",selectedStatesForSelect2,(unsigned long)[selectedState count]);
if([selectedState count] > 3){

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Hold On..." message:@"You can't select more than three states." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[selectedState removeObject:text];
[selectedStatesId removeObject:[NSString stringWithFormat:@"%ld", (long)indexings]];
}

}
}


enter image description here

That seen like this. When user select first time it works very good. But after closing the table view, when user again open the state list I want to show the previous selected item. Thats not working properly. I'm using a library for check "CRMultiRowSelector".Please help me.

Answer

I will give what you ask.I tried many times this.It works perfectly

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableViewCheckMarkPreviousSelectionUpdate;
@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
{
   NSMutableArray *arrProductSelection,*arrProductSelectDeSelectCheckMark;
   NSArray *arrayFetchFromDefaults;
}

@end

@implementation ViewController

@synthesize tableViewCheckMarkPreviousSelectionUpdate;

- (void)viewDidLoad
{
   [super viewDidLoad];
   arrProductSelection = [[NSMutableArray alloc]initWithObjects:@"iPhone",@"iPad",@"iPod",@"iTV",@"iWatch",@"iMac",nil];
}
- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
  NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  arrayFetchFromDefaults = [userDefaults objectForKey:@"selectedcheckmark"];
  arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]initWithArray:arrayFetchFromDefaults];
  if(arrProductSelectDeSelectCheckMark.count == 0)
  {
     arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]init];
     for(int j=0;j<[arrProductSelection count];j++)
     {
        [arrProductSelectDeSelectCheckMark addObject:@"deselected"];
     }
   }
   [tableViewCheckMarkPreviousSelectionUpdate reloadData];
}

#pragma mark - UITableViewDataSource Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return arrProductSelection.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSString *strCell = @"cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
   if(cell==nil)
   {
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
   }
   if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"deselected"])
     cell.accessoryType = UITableViewCellAccessoryNone;
   else
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
   cell.textLabel.text = [arrProductSelection objectAtIndex:indexPath.row];
   return cell;
}

#pragma mark - UITableViewDelegate Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    @try
    {
        CGPoint touchPoint = [cell convertPoint:CGPointZero toView:tableViewCheckMarkSelectionUpdate];
        NSIndexPath *indexPath = [tableViewCheckMarkSelectionUpdate indexPathForRowAtPoint:touchPoint];
        NSLog(@"%@",arrProductSelectDeSelectCheckMark);
        if([arrProductSelectDeSelectCheckMark count]==0)
        {
           for(int i=0; i<[arrProductSelection count]; i++)
           {
             [arrProductSelectDeSelectCheckMark addObject:@"deselected"];
           }
        }
        if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"deselected"])
        {
           cell.accessoryType = UITableViewCellAccessoryCheckmark;
           [arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"selected"];
        }
        else
        {
           cell.accessoryType = UITableViewCellAccessoryNone;
           [arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"deselected"];
        }

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:arrProductSelectDeSelectCheckMark forKey:@"selectedcheckmark"];
        [defaults synchronize];
      }
   @catch (NSException *exception) {
    NSLog(@"The exception is-%@",exception);
   }
}
@end
Comments