【问题标题】:Create a Sectioned UITableView using NSMutableArray of NSDictionaries使用 NSDictionaries 的 NSMutableArray 创建分段 UITableView
【发布时间】:2014-03-17 23:06:22
【问题描述】:

我有一个 NSMutableArrayNSDictionaries,我正在尝试对其进行格式化,以便与具有基于 NSDictionary keys 之一的部分的 UITableView 一起使用。

我的NSDicitonary 有两个值

Name
Key

键是数字 1 - N 的 NSString

我已经设法创建了一个唯一值的NSArray,用于填充每个部分的标题,但是我不知道如何拆分 NSDictionariesinto anNSMutableArray 的 NSDictionaries 的 NSMutableArray ` .. 如果这有意义的话。

更新: 为了给我的问题添加更多细节,我有一个 NSMutableArray 的 NSDictionaries。

{
 Name: Jack
 Key: 1
}
{
 Name: John
 Key: 1
}
{
 Name: Jack
 Key: 1
}
{
 Name: Jack
 Key: 2
}
{
 Name: Jack
 Key: 3
}
{
 Name: Sean
 Key: 3
}
{
 Name: Sally
 Key: 3
}

我想在这样的 UITableView 中显示它

Section 1
- Jack
- John
- Jack
Section 2
- Jack
Section 3
- Jack
- Sean
- Sally

我已经确定我需要做的事情的领域是

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

我已经设法为部分创建了一个 uniqueArray

uniqueKeys = [xmlMutableArray valueForKeyPath:@"@distinctUnionOfObjects.Key"];
    uniqueKeys = [uniqueKeys sortedArrayUsingComparator:^(id obj1, id obj2) {
        return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
    }];

现在我处于如何查找每个部分中的行数然后将 NSDictionary 传递给 cellforrow 中的对象以在单元格中显示正确值的情况?

任何帮助将不胜感激。

【问题讨论】:

    标签: ios objective-c uitableview nsmutablearray nsdictionary


    【解决方案1】:

    希望对您有所帮助:

    // testing
    NSMutableArray *test = [[NSMutableArray alloc]init];
    NSArray *testSample = @[@{@"1": @"hello1",@"2":@"world1",@"3":@"!1"},
                              @{@"1": @"hello2",@"2":@"world2",@"3":@"!2"},
                              @{@"1": @"hello3",@"2":@"world3",@"3":@"!3"}];
    [test addObjectsFromArray:testSample];
    NSMutableArray *sortedTestArray = [[NSMutableArray alloc] init];
    NSLog(@"%@",test);
    
    //get the max length of the nsdictionary
    NSDictionary *item0 = [test objectAtIndex:0];
    NSInteger maxLength = [[item0.allKeys lastObject] integerValue];
    //key start from @"1"
    for (int a = 1; a <= maxLength; a++) {
    NSString *keyNumber = [NSString stringWithFormat:@"%d", a];
    NSArray *sortedItem = [test valueForKey:keyNumber];
    //the index starts from 0
    [sortedTestArray insertObject:sortedItem atIndex:a-1];
    }
    //now the array will be like this,so the item in index 0 will be the key @"1"'s items. index 1 will be the key @"2"'s items...
    NSLog(@"%@",sortedTestArray);
    

    它的原始数据:

    (
            {
            1 = hello1;
            2 = world1;
            3 = "!1";
        },
            {
            1 = hello2;
            2 = world2;
            3 = "!2";
        },
            {
            1 = hello3;
            2 = world3;
            3 = "!3";
        }
    )
    

    排序后的输出:

    (
            (
            hello1,
            hello2,
            hello3
        ),
            (
            world1,
            world2,
            world3
        ),
            (
            "!1",
            "!2",
            "!3"
        )
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      相关资源
      最近更新 更多