我有一个基于导航的 iOS 应用程序。在我的 RootViewController
,用户会看到从 1 到 18 的章节。当他们选择一个单元格时,它会将他们带到 VersesViewController
,在那里他们看到了从第 1 节到其他任何内容的另一个列表。根据他们选择的章节,他们会看到不同数量的经文。
我想修改 NSArray
的内容在 VersesViewController
当用户在 RootViewController
中选择一个单元格时看法。我怎样才能做到这一点?
这里是 RootViewController
代码:
#import "RootViewController.h"
@implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Chapters";
chapterList = [[NSMutableArray alloc] init];
for (int i = 1; i<19; i++)
{
[chapterList addObject:[NSString stringWithFormat:@"Chapter %d", i]];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [chapterList count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [chapterList objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
VersesViewController *detailViewController = [[VersesViewController alloc] initWithNibName:@"VersesViewController" bundle:nil];
if ([[chapterList objectAtIndex:indexPath.row] isEqual:@"Chapter 9"]) {
[detailViewController setNumberOfVerses:18];
} else {
[detailViewController setNumberOfVerses:72];
}
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.title = nil;
}
- (void)dealloc
{
[super dealloc];
[chapterList release];
}
@end
请注意,我是 Obj-C 的 n00b,所以我需要非常简单的答案。谢谢。
最佳答案
您需要更复杂的数据存储。您可以制作字典数组来描述所有章节的数据。
- (void)viewDidLoad
{
chapterList = [[NSMutableArray alloc] init];
...
chapterInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"Chapter 9", @"title", [NSNumber numberWithInt:18], @"numberOfVerses", nil];
[chapterList addObject:chapterInfo];
chapterInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"Chapter 10", @"title", [NSNumber numberWithInt:72], @"numberOfVerses", nil];
[chapterList addObject:chapterInfo];
...
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *chapterInfo = [chapterList objectAtIndex:indexPath.row];
cell.textLabel.text = [chapterInfo objectForKey:@"title"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
VersesViewController *detailViewController = [[VersesViewController alloc] initWithNibName:@"VersesViewController" bundle:nil];
NSDictionary *chapterInfo = [chapterList objectAtIndex:indexPath.row];
[detailViewController setNumberOfVerses:[[chapterInfo objectForKey:@"numberOfVerses"] intValue]];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
最好的方法是创建包含所有章节描述的 plist 文件,然后像这样加载它:
NSString *path = [[NSBundle mainBundle] pathForResource:@"chapters" ofType:@"plist"];
NSArray *chaptersList = [[NSArray alloc] initWithContentsOfFile:path];
关于objective-c - 如何根据他们选择的上一个 View 中的哪个单元格自定义用户看到的 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6328018/