来自Plist的数据在UITableView中被错误地命令
我有存储在plist中的数据,但是当我把它拉到我的UITableView它由于某种原因被重新sorting。 这里是我的表视图数据源方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.lunch_Dinner count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [[self.lunch_Dinner allKeys] objectAtIndex:section]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSString *typeOfEntree = [self tableView:tableView titleForHeaderInSection:section]; return [[self.lunch_Dinner valueForKey:typeOfEntree] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"EntreeCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell... NSString *typeOfEntree = [self tableView:tableView titleForHeaderInSection:indexPath.section]; NSString *entree = [[self.lunch_Dinner valueForKey:typeOfEntree] objectAtIndex:indexPath.row]; cell.textLabel.text = entree; return cell; }
这是plist的顺序:
- 开胃菜
- 汤类
- 意大利面
- 比萨饼
- 特价
这是编译后UITableView中的结果顺序:
- 比萨饼
- 汤类
- 开胃菜
- 意大利面
- 特价
任何帮助将是伟大的! 提前致谢。
如果你想在plist中存储表格,最好考虑下面的结构:
NSArray *sections = [NSArray arrayWithObjects: [NSDictionary dictionaryWithObject: [NSArray arrayWithObjects:row_1, row_2, ... , nil] forKey:@"section name 1"], [NSDictionary dictionaryWithObject: [NSArray arrayWithObjects:row_1, row_2, ... , nil] forKey:@"section name 2"], ... [NSDictionary dictionaryWithObject: [NSArray arrayWithObjects:row_1, row_2, ... , nil] forKey:@"section name N"], nil];
这个代码表示很容易重现为plist。 像下面的例子那样创build它
这个结构可以很容易地用作UITableView数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.datasource count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSDictionary *dict = [self.datasource objectAtIndex:section]; return [dict.allKeys lastObject]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSDictionary *dict = [self.datasource objectAtIndex:section]; return [dict.allValues.lastObject count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dict = [self.datasource objectAtIndex:indexPath.section]; id cellPresenter = [dict.allValues.lastObject objectAtIndex:indexPath.row]; ... }
它看起来像lunch_Dinner
是一个NSDictionary
。 NSDictionary
是无序的,所以你不能保证它们的特定输出顺序。
在这里有一个键/值对的确切数值是很难的,但是一个select是保持一个单独的NSArray
正确的顺序,并使用它来正确地填充事物。