【问题标题】:How to arrange UITableView Sections titles based on plist keys ?如何根据 plist 键排列 UITableView Sections 标题?
【发布时间】:2014-01-31 00:48:38
【问题描述】:

我有一个 plist,带有键值,键是字符串,值也是字符串。 我不希望 uitableview 部分标题的输出根据 plist 中的键 .也就是说,我的意思是 key1-应该是 section-1 的标题,key-2 应该是 section-2 的标题,很快。 这是我的示例 plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Settings</key>
    <array>
        <string>one-one</string>
        <string>one-two</string>
    </array>
    <key>Notifications</key>
    <array>
        <string>two-one</string>
        <string>two-two</string>
    </array>
    <key>Info</key>
    <array>
        <string>three_one</string>
        <string>three_two</string>
        <string>three_three</string>
    </array>
</dict>
</plist>

这是我尝试在 viewDidLoad 中重新排列的代码

_

data = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"]];
    NSArray *first = [_data valueForKey:@"Settings"];
    NSArray *second = [_data valueForKey:@"Notifications"];
    NSArray *third = [_data valueForKey:@"Info"];
    NSDictionary *temp =[[NSDictionary alloc]
                         initWithObjectsAndKeys:first, @"Settings",second, @"Notificatitons",third, @"Info",  nil];
    _data = temp;

这是表数据源的配置

#pragma mark - data source 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_data count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[_data allKeys] objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    NSString *continent = [self tableView:_testTable titleForHeaderInSection:section];
    return [[_data valueForKey:continent] count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *rekebishaCell = @"RekebishaCell";
    NSString *continent = [self tableView:_testTable titleForHeaderInSection:indexPath.section];
    NSString *sub_settings = [[_data valueForKey:continent] objectAtIndex:indexPath.row];

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: rekebishaCell];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                       reuseIdentifier:rekebishaCell];
    }
    // config cell ui
    //cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.text = sub_settings;

    return cell;
}

到目前为止,我得到了这个

我需要这个输出 1-设置 2-通知 3-信息

我很感激的任何帮助,可能是我盯着屏幕看了很长时间,我的大脑错过了一些东西,:P

【问题讨论】:

    标签: ios iphone objective-c uitableview


    【解决方案1】:

    NSDictionary 不以有序的方式存储键/值。如果您想订购它们,您需要将它们全部取出并使用不同的结构订购它们,很可能是NSArray

    请参阅此 S.O.答案:

    NSDictionary's allKeys array messed up - not in the order they are in the dictionary?

    【讨论】:

    • ...这正是我面临的问题,我会考虑重新组织我的数据结构,我的 NSArray 是一种方法,谢谢你的提示,但让我们等待可能有人已经完成了 b4。
    • 我认为没有人以您所描述的方式使用NSDictionary。我相当确定您目前唯一的解决方案是使用字典以外的其他内容或将记录拉出,按照您的意愿对它们进行排序,然后然后将它们用作表。
    • ...Aaron 我只是想保持 plist 中键的顺序。由于 NSDictionary 不维护键的顺序,这就是我面临的问题!,我正在寻找这篇文章可能会引导我走向正确的道路! cocoawithlove.com/2008/12/…
    • 如果这是您遇到的问题,您还必须意识到您不能使用字典,而需要使用数组。是的,该链接中的子类可以工作。我以前用过。请注意,如果您通读实现,您会发现他的解决方案仍然使用NSArray 来维护排序。 NSArray 订购就是您的答案。
    猜你喜欢
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 2019-10-05
    相关资源
    最近更新 更多