I am new in iOS and I am facing problem regarding to multiple selection on table view.
When I am using pushview controller selected value get deselect
I am using code like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text=[NSString stringWithFormat:#"%#",[reportshortActivityarray objectAtIndex:indexPath.row]];
if([Selectedarray containsObject:[reportshortActivityarray objectAtIndex:indexPath.row]])
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(![Selectedarray containsObject:[reportshortActivityarray objectAtIndex:indexPath.row]]){
[Selectedarray addObject:[reportshortActivityarray objectAtIndex:indexPath.row]];
txtactivity.text = [Selectedarray componentsJoinedByString:#","];
DefaultActivityString=txtactivity.text;
}
NSLog(#"Selected Value =%#",txtactivity.text);
if(![SelectedIDarray containsObject:[reportidActivityarray objectAtIndex:indexPath.row]]){
[SelectedIDarray addObject:[reportidActivityarray objectAtIndex:indexPath.row]];
lblactivity.text = [SelectedIDarray componentsJoinedByString:#","];
}
NSLog(#"Selected Value =%#",lblactivity.text);
[[activitytable cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
activitytable.hidden=NO;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([Selectedarray containsObject:[reportshortActivityarray objectAtIndex:indexPath.row]]){
[Selectedarray removeObject:[reportshortActivityarray objectAtIndex:indexPath.row]];
txtactivity.text = [Selectedarray componentsJoinedByString:#","];
}
if([SelectedIDarray containsObject:[reportidActivityarray objectAtIndex:indexPath.row]]){
[SelectedIDarray removeObject:[reportidActivityarray objectAtIndex:indexPath.row]];
lblactivity.text = [SelectedIDarray componentsJoinedByString:#","];
}
NSLog(#"Selected Value =%#",lblactivity.text);
[[activitytable cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
activitytable.hidden=NO;
}
To do multiple selection
And on Button
-(IBAction)btnaddClick:(id)sender
{
IncidentHSQE *inc=[[IncidentHSQE alloc] initWithNibName:#"IncidentHSQE" bundle:nil];
[self.navigationController pushViewController:inc animated:YES];
}
selected data get removed.
How can I recover that selected data.
Thanks in Advance!
Two bug in this code.
First one when i delete Row in tableview delete last row not selected row.
Second Bug is when try to delete more than one row that time will delete successfully but when reload tableview Only one row deleted. Rest row will appear again.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return result.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath : indexPath];
if (!(cell == nil)) {
name = (UILabel *)[cell viewWithTag:10];
name.text = [result objectAtIndex : indexPath.row];
}
name.tag=indexPath.row;
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (IBAction)action:(id)sender {
[_myTableView setEditing:YES animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath : indexPath];
if (!(cell == nil)) {
name = (UILabel *)[cell viewWithTag:10];
name.text = [result objectAtIndex : indexPath.row];
}
name.tag=indexPath.row;
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) {
[result removeObjectAtIndex:indexPath.row];
[tableView reloadData];
[spinner startAnimating];
NSString *id = [profile objectForKey:#"user_id"];
NSString *tb =tid;
NSString *string = [NSString stringWithFormat:#"userid=%#&topiid=%#",id,tb];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"myDeleteUrl"]];
NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setHTTPMethod:#"POST"];
[request setHTTPBody : data];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration
defaultSessionConfiguration]]; [[session dataTaskWithRequest : request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *JSON= [NSJSONSerialization JSONObjectWithData :data options :NSJSONReadingMutableContainers error: nil];
NSArray *alertArray = [JSON objectForKey:#"deletebookmark"];
for (NSDictionary *alert in alertArray ){
if ([[alert objectForKey:#"status"] isEqualToString:#"done"]) {
NSLog(#"done");
[spinner stopAnimating];
}
else{
NSLog(#"notDone");
[spinner stopAnimating];
}
}
}]resume];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath : indexPath];
if (!(cell == nil)) {
name = (UILabel *)[cell viewWithTag:10]; name.text = [result objectAtIndex : indexPath.row];
}
name.tag=indexPath.row;
return cell;
}
In this datasource method, you have used a "if" condition, and due to your cell is reusable cell, so you have to use the else part too in this data source method.
Like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath : indexPath];
if (!(cell == nil)) {
name = (UILabel *)[cell viewWithTag:10]; name.text = [result objectAtIndex : indexPath.row];
}
else{
name.tag=indexPath.row;
}
return cell;
}
Thanks
Try this way
[result removeObjectAtIndex:indexPath.row];
[tableView reloadData];// comment this line above code
[spinner stopAnimating];
//After that you can add
[tableView reloadData]; // above stopanimating line.
Add this cellForRowAtIndexPath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath : indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"cell"];
}
name = (UILabel *)[cell viewWithTag:10];
name.text = [result objectAtIndex : indexPath.row];
name.tag=indexPath.row;
return cell;
}
I have used UITableViewCellEditingStyle checkbox in my tableview for multiple selection. I use the uisearchbar to filter and then mark the cells..but when searchbar is closed and back to the main unfiltered table.. the previous selections disappear.
- (void)viewDidLoad {
[super viewDidLoad];
[tableview setEditing:YES animated:YES];
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(isfiltered==YES){
return [filteredarr count];
}
else{
if(arr==nil)return 0;
return [arr count] ;}
}
-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID=#"cellID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
if(cell==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
if(isfiltered==YES){
cell.textLabel.text=[filteredarr objectAtIndex:indexPath.row];
}
else {
cell.textLabel.text=[arr objectAtIndex:indexPath.row];
}
cell.tintColor=[UIColor grayColor];
return cell;
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if(searchText.length==0){
isfiltered=NO;
}
else{
isfiltered=YES;
filteredarr=[NSMutableArray array];
for(NSString *str in arr){
NSRange textrange=[str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(textrange.location != NSNotFound){
[filteredarr addObject:str];
}
}
}
[tableview reloadData];
}
Is there anyway to keep the selections?
I have the exact same problem as the question posted here.
Which looks exactly like this gif.
I'm not which method to begin troubleshooting the problem. I've sub-classed the UICell and have used the standard UICell, both with the same results. I've also tried to set the image in the cell to nil when it edit mode: cell.imageView.image = nil;
Any thoughts would be appreciated, I can post more code if needed...
// Cell For Row At Index Path
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Item cell func");
static NSString *CellIdentifier = #"detailCell";
MCTableViewCell *cell = nil;
if (cell == nil)
{
cell = [[MCTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
Item * record = [_itemsArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageWithData:record.photo];
cell.textLabel.text = [NSString stringWithFormat:#" %#", record.itemName];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
//cell.shouldIndentWhileEditing = NO;
NSLog(#"Detail Record.Name %#", [_itemsArray objectAtIndex:indexPath.row]);
return cell;
}
// Editing Cells
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Begin editStyle Func");
if (editingStyle == UITableViewCellEditingStyleDelete) {
//UITableViewCellEditingStyleNone
// 1
[tableView beginUpdates];
// Delete the row from the data source
NSLog(#"after Updates editStyle Func");
// Delete item from Table
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
NSLog(#"after Row Animation editStyle Func");
// Delete Item from Database
[self.managedObjectContext deleteObject:[self.itemsArray objectAtIndex:indexPath.row]];
NSError *error;
if (![self.managedObjectContext save:&error]) {
NSLog(#"Save: %#", [error localizedDescription]);
}
[self showObjects];
NSLog(#"End of if stmnt");
// 5
[tableView endUpdates];
}
NSLog(#"End of Function");
}
This problem seems to happen in ios7.
in ios8, works fine. so, this might be a bug.
To work around, add your own UIImageView to cell and use it instead of UITableVewCell's imageView.
So I've implemented a UIViewController with a tableview, and basically it loads as a set of "filters" for my uicollectionview.
Now, when I click on the checkmarks in my tableview, it "filters" my cells accordingly, but now when I reload the view again I want to display the most recent "checkmarks" I've used, or "filters."
I have seen this being implemented with NSUserDefaults, but I have not been able to successfully implement this.
If anyone could help me, that will be greatly appreciated.
CODE
FiltersViewController.m:
#import "FiltersViewController.h"
#interface FiltersViewController ()
#property (nonatomic, strong) NSMutableSet *selectedRowObjects;
//#property (nonatomic, strong) NSArray *filters;
#end
#implementation FiltersViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.selectedRowObjects = [NSMutableSet setWithCapacity:10];
}
- (IBAction)filtersSelected:(id)sender {
[self.delegate filtersSelected:self.selectedRowObjects];
}
- (IBAction)cancelFilterSelection:(id)sender {
[self.delegate filterSelectionCancelled];
}
- (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];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"filter" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"%u", indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *obj = cell.textLabel.text;
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedRowObjects removeObject:obj];
}
else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedRowObjects addObject:obj];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#end
you need to check in cellForRowAtIndexPath also. Write this code in this
if([[NSUserDefaults standardUserDefaults] objectForKey:[self getKeyForIndex:indexPath.row]])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
And yes don't forget to call this method in didSelectRowAtIndexPath
[self checkedCellAtIndex:indexPath.row];
Enjoy.
You did almost everything right. You just need to put your logic for reading the NSUserDefault values (for the checked boxes) in the cellForRowAtIndexPath delegate method. That is the method that draws the UITableViewCells when they are displayed on screen. Something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"filter" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"%u", indexPath.row];
if ([self getCheckedForIndex:indexPath.row])
{
//code to set checkbox
}
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleTableIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = ArrayOfObject[indexPath.row];
NSUserDefaults *ud =[NSUserDefaults standardUserDefaults];
if([[ud objectForKey:#"selectedObjectKey "]isEqualToString:ArrayOfObject[indexPath.row]])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
You have implemented perfectly. You just need to modify you cellForRowAtIndexPath Method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"filter" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"%u", indexPath.row];
BOOL checked = [self getCheckedForIndex:indexPath.row];
if(checked)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
Also Call [self checkedCellAtIndex:indexPath.row]; from didSelectRowAtIndexPath method of UITableView.
The best way to save tableView CheckMark with NSUserDefaults
#interface TableViewController (){
NSArray *TableTitles;
NSMutableArray *SelectedRows;
}
viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
// table view array
TableTitles = [NSArray arrayWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10", nil];
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
SelectedRows = [NSMutableArray arrayWithArray:[userDef objectForKey:#"SelectedRows"]];
}
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = TableTitles[indexPath.row];
NSNumber *obj = [NSNumber numberWithInteger:indexPath.row];
if ([SelectedRows containsObject:obj])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSNumber *obj = [NSNumber numberWithInteger:indexPath.row];
if ([SelectedRows containsObject:obj])
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
[SelectedRows removeObject:obj];
[tableView reloadData];
}else{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[SelectedRows addObject:obj];
[tableView reloadData];
}
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
[userDef setObject:SelectedRows forKey:#"SelectedRows"];
[userDef synchronize];
}
updated ...
i updated all code with a new way , it's the best way to save TableView CheckMarks and it's now easy and efficient, now only one array is used for saving and loading , code reduced too much :)
You should load data in NSUserDefaults while call cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"filter" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"%u", indexPath.row];
//You should set accessoryType here by NSUserDefaults data
if ([self getCheckedForIndex:indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
I think what you are looking for is to get the current state of the table for the filters when you enter and exit the view. You need to utilize the - (void)viewWillDisappear:(BOOL)animated to save the current state of the table to your NSUserDefaults, and then use - (void)viewDidLoad and - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to set the checkmark when the table reloads.
#interface FiltersViewController () {
NSMutableArray *_filterStates;
}
#end
- (void)viewDidLoad {
filterStates = [[NSMutableArray alloc] init];
// get state of your current filters in NSUserDefaults, these should be stored as [NSNumber numberWithBool:]
}
- (void)viewWillDisappear:(BOOL)animated {
// store all of your filters into your NSUserDefaults as [NSNumber numberWithBool:]
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// get your cell from the table
BOOL isChecked = [[_filterStates objectAtIndex:indexPath.row] boolValue];
// set the checkmark based on the current state for this filter
}