我对 iOS 开发很陌生(正如您将在下面的代码中看到的那样)。我喜欢通过操纵现有代码做一些不同的事情来帮助自己学习新语言。不过,我对这个有点空白。在表格视图中每个部分的末尾,它使用的数据会重置并重新开始,而不是继续。谁能告诉我这里的问题出在哪里?
#import "RootViewController.h"
#import "DataController.h"
#import "DetailViewController.h"
#import "Play.h"
@implementation RootViewController
@synthesize dataController;
@synthesize play;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.title = NSLocalizedString(@"Plays", @"Master view navigation title");
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Only one section.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
/*
The number of rows varies by section.
*/
NSInteger rows = 0;
switch (section) {
case 0:
rows = 3;
break;
case 1:
rows = 1;
break;
case 2:
rows = 2;
break;
default:
break;
}
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
static NSString *CellIdentifier = @"PlayCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Get the object to display and set the value in the cell.
Play *playAtIndex = [dataController objectInListAtIndex:indexPath.row];
cell.textLabel.text = playAtIndex.title;
return cell;
}
// Section header titles
#pragma mark -
#pragma mark Section header titles
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section {
NSString *secttitle = nil;
switch (section) {
case 0:
secttitle = NSLocalizedString(@"Comedy", @"Comedy section title");
break;
case 1:
secttitle = NSLocalizedString(@"Action", @"Action section title");
break;
case 2:
secttitle = NSLocalizedString(@"Drama", @"Drama section title");
break;
default:
break;
}
return secttitle;
}
// End of section header titles
#pragma mark -
#pragma mark Table view selection
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
/*
When a row is selected, the segue creates the detail view controller as the destination.
Set the detail view controller's detail item to the item associated with the selected
row.
*/
if ([[segue identifier] isEqualToString:@"ShowSelectedPlay"]) {
NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
DetailViewController *detailViewController = [segue destinationViewController];
detailViewController.play = [dataController objectInListAtIndex:selectedRowIndex.row];
}
}
@end
感谢您到目前为止的回复。Detail 视图工作正常,从您目前的反馈来看,似乎是因为没有评估此代码,但我似乎无法确定将其合并到主视图中的哪个位置。
NSString *cellText = nil;
switch (indexPath.section) {
case 0:
cellText = play.date;
break;
case 1:
cellText = play.genre;
break;
case 2:
cellText = [play.characters objectAtIndex:indexPath.row];
break;
default:
break;
}
cell.textLabel.text = cellText;
return cell;