为什么每次都会重置Section数据?

时间:2012-08-15 17:10:52

标签: ios xcode

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

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

这一行:

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

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