[英]Selecting Different Cells in One TableView with 2 Data sources
在一个视图 controller 上,我有一个UITableView
和一个UISegmentedControl
在它的顶部。 表视图由 NSArray 填充,该 NSArray 从几种不同的方式之一收集文件。 0 段从 2 个文件夹中提取所有 PDF。 1 段从文件名末尾包含“Kids.pdf”的文件夹中提取所有 PDF。 我想要做的是允许用户创建他们喜欢的歌曲列表并保存。 当段更改时,我成功地显示了我想要的内容,但问题是复选标记仍然存在。 所以,如果我 select 从第一个段的顶行开始,当我 go 到另一个段时,它仍然显示一个也被选中。 我 go 如何解决这个问题,以便在每个段之间来回切换将保留用户实际选择的复选标记?
- (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];
}
最后是段的代码。
-(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];
}
}
它非常简单。 当您更改段选择(更改数据源)时,选中标记的单元格将被重用,因此它显示最新的 state 的单元格。您需要重置 cellForRowAtIndexPath 方法中的所有状态:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
if (values[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
一般来说,记住。 如果单元格中有if
,则还应该有else
case,否则会出现重用问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.