Reputation:
I am Newbie in iOS Development. I want to make an Application that Contain UITableview. I want to Show when user Select UITableView Cell then it Like as Check marked it is Worked as i want but now i want to Show when User Back to Other View And Come back to Table View then it Shows Your Past Selection Of Table View Cell Please Give me Solution For that.
I Show in Stack Overflow it is Done by NSUSerdefault i find a code for it and Set like as But it is Not Working for me Please Give me Solution.
Here my UITableview didSelectRowAtIndexPath
Method.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CategoryCustumCell *cell = (CategoryCustumCell *)[tableView cellForRowAtIndexPath:indexPath];
NSString *selectedCell= cell.categoryLabel.text;
NSLog(@"Category label %@",selectedCell);
NSString *text=[self.categoryArray objectAtIndex:indexPath.section];
if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath])
{
[self.selectedCells removeObject:indexPath];
[self.selecedStates removeObject:text];
cell.accessoryType = UITableViewCellAccessoryNone;
} else
{
[self.selectedCells addObject:indexPath];
[self.selecedStates addObject:text];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
NSLog(@"%@", self.selecedStates);
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
[userdefaults setObject:[NSString stringWithFormat:@"%d",indexPath.section] forKey:@"lastIndexPathUsed"];
[userdefaults synchronize];
}
And Also Code for UITableView cellForRowAtIndexPath
-(CategoryCustumCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
CategoryCustumCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"CategoryCustumCell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
cell.categoryLabel.text=[self.categoryArray objectAtIndex:indexPath.section];
cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
And I write to Fetch NSUserdefault Data in viewDidLoad like as
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.lastIndexPath = [defaults objectForKey:@"lastIndexPathUsed"];
and i Define in my .h
file
@property (nonatomic,retain) NSIndexPath *lastIndexPath;
But i not Get Selection on User Come Back to the View Please Give me Solution for it.
Upvotes: 1
Views: 1729
Reputation: 657
Try this.
-(CategoryCustumCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
CategoryCustumCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"CategoryCustumCell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
cell.categoryLabel.text=[self.categoryArray objectAtIndex:indexPath.section];
if (indexPath.section==self.lastIndexPath) {
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else{
cell.accessoryType=UITableViewCellAccessoryNone;
}
return cell;
}
Upvotes: -1
Reputation: 6570
Create a property like below
@interface ViewController ()
//I added this property to keep track of the selected row
@property (strong,nonatomic) NSIndexPath *selectedPath;
@end
Check if it's nil or value is present in indexpath
- (void)viewWillAppear:(BOOL)animated
{
//I added this if clause to select the row that was last selected
if (self.selectedPath != nil) {
[TABLENAME selectRowAtIndexPath:self.selectedPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
Set the value in didSelect
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// set here
self.selectedPath = indexPath;
}
Upvotes: 2