Ios 为什么每次都重置区段数据?

Ios 为什么每次都重置区段数据?,ios,xcode,Ios,Xcode,我对iOS开发非常陌生(从下面的代码中可以看到)。我喜欢通过操纵现有代码来做一些不同的事情来帮助自己学习新的语言。不过,我在这方面有点空白。在表视图中每个部分的末尾,它使用的数据将重置并重新启动,而不是继续。谁能告诉我这里的毛病在哪里吗 #import "RootViewController.h" #import "DataController.h" #import "DetailViewController.h" #import "Play.h" @implementation RootV

我对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
谢谢你的回复。细节视图工作得很好,从您目前的反馈来看,这似乎是因为没有对该代码进行评估,但我似乎无法确定将其合并到主视图的何处

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;

因为您只使用indexPath.row来获取单元格内容。您的数据模型也应该考虑索引路径。
Play *playAtIndex = [dataController objectInListAtIndex:indexPath.row]; 
cell.textLabel.text = playAtIndex.title; 
i、 e.您有一个一维数据模型…

这一行:

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

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

我和@Husker Jeff同时回答了正确的答案。您不能在注释中标记{}代码,但可以更新您的问题。您需要修改dataController模型以支持类似[dataController ObjectInListIndex:row forSection:section]的内容。