来自Plist的数据在UITableView中的顺序不正确 [英] Data from Plist is Incorrectly Ordered in UITableView

查看:85
本文介绍了来自Plist的数据在UITableView中的顺序不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储在plist中的数据,但是当我将其拉到UITableView时,由于某种原因,它会重新排序.这是我的表格视图数据源方法:

I have data that is stored in a plist but when I pull it to my UITableView it gets reordered for some reason. Here are my table view data source methods:

- (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的顺序:

Here is the order of the plist:

  • 开胃菜
  • 意大利面
  • 比萨饼
  • 特色菜

这是编译后的UITableView中的结果顺序:

And this is the resulting order in the UITableView after being compiled:

  • 比萨饼
  • 开胃菜
  • 意大利面
  • 特色菜

任何帮助都会很棒! 预先感谢.

Any help would be great! Thanks in advance.

推荐答案

如果要将表存储在plist中,最好考虑以下结构:

If you want to store table in plist it is better to consider the following structure:

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.如下例创建

This code representation is easy to reproduce as plist. Create it as in example below

此结构可以轻松用作UITableView数据源

This structure can be easily used as UITableView datasource

- (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];
    ...
}

这篇关于来自Plist的数据在UITableView中的顺序不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆