0

我对 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;
4

2 回答 2

0

因为您只使用 indexPath.row 来获取单元格内容。您的数据模型还应该考虑 indexPath.section:

Play *playAtIndex = [dataController objectInListAtIndex:indexPath.row]; 
cell.textLabel.text = playAtIndex.title; 

即你有一个一维数据模型......

于 2012-08-15T17:17:13.387 回答
0

这一行:

Play *playAtIndex = [dataController objectInListAtIndex:indexPath.row];

仅考虑当前 indexPath 的行,该行也在每个部分的开头重置为零。您需要使用数组数组或以其他方式考虑前面部分中的行以获得正确的索引。

于 2012-08-15T17:17:32.343 回答