-2

I have a list of items/cells in TableView. Table is set to allow multiple selection.

So, i have these methods to select and deselect.

@interface NotificationsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,UIScrollViewDelegate>
{
NSMutableArray *checkedIndexPaths;
}

-(void)viewDidLoad{
//Setup default array
checkedIndexPaths = [[NSMutableArray alloc]init];
}

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

NotificationsTableViewCell *cell = (NotificationsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imgIcon.image=[UIImage imageNamed:@"select-icon"];

Notifications* selected = notifications[indexPath.section];
selectedGroup = selected.NotificationsId;

[checkedIndexPaths addObject:selectedGroup];

}

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

NotificationsTableViewCell *cell = (NotificationsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imgIcon.image=[UIImage imageNamed:@""];

Notifications* selected = notifications[indexPath.section];
selectedGroup = selected.NotificationsId;

[checkedIndexPaths addObject:selectedGroup];

}

I want to add each object into array during multiple selection. But with this current coding, it will overwrite the same one, and i have no idea how can i make it multiple object array to store in each click of cell. Thank you for your helps.

Umit Kaya
  • 5,771
  • 3
  • 38
  • 52
  • You should use a for/in loop. And before you ask what that is, if you don't know. Try doing some research. Here is a great example provided by Apple : https://developer.apple.com/library/ios/samplecode/TableMultiSelect/Introduction/Intro.html there's a lot going on with your code that isn't fundamentally efficient. Try cross referencing all your methods against apples and learn from how they implicit structure. – soulshined Jun 29 '15 at 03:52
  • There's also no reason for this `NotificationsTableViewCell *cell = (NotificationsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];` you already know the indexPath from the method itself. Plus you don't actually do anything with the cell – soulshined Jun 29 '15 at 03:54
  • @soulshined i had need to use cell.imgIcon.image=[UIImage imageNamed:@"select-icon"]; thats why i declared the 'cell'. – Umit Kaya Jun 29 '15 at 05:21

1 Answers1

1

You're creating new NSMutableArrays after each method call. Your listArray should be a property of the object, or otherwise visible outside of the scope of these two methods, that you add and remove items from on each method call.

Connor Neville
  • 7,291
  • 4
  • 28
  • 44
  • Thanks. But the problem is in each call it will overwrite on the same object. what mechanism i can use to store multiple? – Umit Kaya Jun 29 '15 at 03:01
  • That is not your problem: your line `NSMutableArray *listArray = [[NSMutableArray alloc] init];` creates a new, blank array each time. That is why, when you select, multiple items, it appears the second overwrote the first. – Connor Neville Jun 29 '15 at 03:02
  • i changed my question, but in this case array can't be called fro viewDidload. it says: 'use of undeclared identifier'. – Umit Kaya Jun 29 '15 at 03:09
  • Google "Objective C properties". Regardless of where you write it, that line is creating a local variable which will only exist in the scope of the method you are inside. You will need a grasp of this concept to do any sort of iOS programming. – Connor Neville Jun 29 '15 at 03:28