On one view controller, I have a single UITableView
and a UISegmentedControl
at the top of it. The table view is populated from an NSArray that collects the files from one of a couple different ways. The 0 segment pulls all PDFs from 2 folders. The 1 segment pulls all PDFs from one folder in which the ending of the filename contains 'Kids.pdf'. What I am trying to do is allow the user to create a list of the songs they like, and save it. I have it successfully showing what I want when the Segment is changed, but the issue is that the Checkmarks stay on. So, if I select the top row from the first segment, when I go to the other segment, it still shows that one selected as well. How can I go about fixing this so that going back and forth between each segment will retain the checkmarks for what the user actually selected?
- (void)viewWillAppear:(BOOL)animated {
self.title = @"Choose YourSongs";
if (filterSongs.selectedSegmentIndex == 0) {
NSBundle *bundle = [NSBundle mainBundle];
self.files = [bundle pathsForResourcesOfType:@"pdf" inDirectory:@"thepdfpowerpoints"];
NSString *documentsDirectoryPath = [self.files objectAtIndex:thepath.row];
self.filenames = [[documentsDirectoryPath lastPathComponent] stringByDeletingPathExtension];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *thefirstNewArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
NSArray *theNewArray = [thefirstNewArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.pdf'"]];
NSMutableArray *names = [NSMutableArray arrayWithCapacity:[self.files count] + [theNewArray count]];
for (NSString *path in self.files) {
[names addObject:[[path lastPathComponent] stringByDeletingPathExtension]];
}
for (NSString *pathagain in theNewArray) {
[names addObject:[[pathagain lastPathComponent] stringByDeletingPathExtension]];
}
//self.files = names;
self.files = [names sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"FILESLOAD%@", self.files);
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
else {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *thefirstNewArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
NSArray *theNewArray = [thefirstNewArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH 'Kids.pdf'"]];
NSMutableArray *names = [NSMutableArray arrayWithCapacity:[theNewArray count]];
for (NSString *path in self.files) {
[names addObject:[[path lastPathComponent] stringByDeletingPathExtension]];
}
for (NSString *pathagain in theNewArray) {
[names addObject:[[pathagain lastPathComponent] stringByDeletingPathExtension]];
}
//self.files = names;
self.files = [names sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"FILESLOAD%@", self.files);
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedCountry = [self.files objectAtIndex:indexPath.row];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [[documentsDirectory stringByAppendingPathComponent:selectedCountry] stringByAppendingString:@".pdf"];
if ([[NSFileManager defaultManager] fileExistsAtPath: pdfPath]) {
//NSLog(@"%@", selectedCountry);
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *addThis = [[pdfPath lastPathComponent] stringByDeletingPathExtension];
NSLog(@"%@", addThis);
values[indexPath.row] = !values[indexPath.row];
if (self.chosen == nil) {
self.chosen = [[NSMutableArray alloc] init];
}
if (values[indexPath.row]) {
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.chosen addObject:addThis];
} else {
newCell.accessoryType = UITableViewCellAccessoryNone;
[self.chosen removeObject:addThis];
}
}
else {
NSString *Documents = [[NSBundle mainBundle] pathForResource:selectedCountry ofType:@"pdf" inDirectory:@"thepdfpowerpoints"];
//NSLog(@"%@", selectedCountry);
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *addThis = [[Documents lastPathComponent] stringByDeletingPathExtension];
NSLog(@"%@", addThis);
values[indexPath.row] = !values[indexPath.row];
if (self.chosen == nil) {
self.chosen = [[NSMutableArray alloc] init];
}
if (values[indexPath.row]) {
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.chosen addObject:addThis];
} else {
newCell.accessoryType = UITableViewCellAccessoryNone;
[self.chosen removeObject:addThis];
}
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
And finally the code for the segment.
-(IBAction) changeSegment {
if (filterSongs.selectedSegmentIndex == 0) {
NSBundle *bundle = [NSBundle mainBundle];
self.files = [bundle pathsForResourcesOfType:@"pdf" inDirectory:@"thepdfpowerpoints"];
NSString *documentsDirectoryPath = [self.files objectAtIndex:thepath.row];
self.filenames = [[documentsDirectoryPath lastPathComponent] stringByDeletingPathExtension];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *thefirstNewArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
NSArray *theNewArray = [thefirstNewArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.pdf'"]];
NSMutableArray *names = [NSMutableArray arrayWithCapacity:[self.files count] + [theNewArray count]];
for (NSString *path in self.files) {
[names addObject:[[path lastPathComponent] stringByDeletingPathExtension]];
}
for (NSString *pathagain in theNewArray) {
[names addObject:[[pathagain lastPathComponent] stringByDeletingPathExtension]];
}
//self.files = names;
self.files = [names sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView reloadData];
}
else {
self.files = nil;
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory2 = [paths2 objectAtIndex:0];
NSArray *thefirstNewArray2 = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory2 error:NULL];
NSArray *theNewArray2 = [thefirstNewArray2 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH 'Kids.pdf'"]];
NSLog(@"Kids newArray %@", thefirstNewArray2);
NSMutableArray *names2 = [NSMutableArray arrayWithCapacity:[theNewArray2 count]];
for (NSString *pathagain2 in theNewArray2) {
[names2 addObject:[[pathagain2 lastPathComponent] stringByDeletingPathExtension]];
}
//self.files = names;
self.files = [names2 sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView reloadData];
}
}
Its pretty simple. The checkmarked cell is being reused when you change the segment selection (change datasource), hence it shows the cell with the latest state. You need to reset all the states in the cellForRowAtIndexPath method:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
if (values[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
In general, keep in mind. If you have an if
in the cell, you should also have the else
case, otherwise you will come up with the reuse issue.