0

我已经有一个字典数组(用于贷款项目),示例如下:

(
    {
    BIBNo = 291054;
    date = "10/08/2012";
    itemSequence = 000010;
    name = "iPhone application development for IOS 4 / Duncan Campbell.";
    noOfRenewal = 1;
    number = 000291054;
    status = "Normal Loan";
    time = "24:00";
    type = Loans;
},
    {
    BIBNo = 187883;
    date = "13/08/2012";
    itemSequence = 000010;
    name = "Positive thinking / Susan Quilliam.";
    noOfRenewal = 2;
    number = 000187899;
    status = "Normal Loan";
    time = "24:00";
    type = Loans;
},
    {
    BIBNo = 290408;
    date = "13/08/2012";
    itemSequence = 000010;
    name = "Sams teach yourself iPhone application development in 24 hours / John Ray.";
    noOfRenewal = 1;
    number = 000290408;
    status = "Normal Loan";
    time = "24:00";
    type = Loans;
})

现在我需要将它们插入到表格中,日期作为部分,行根据日期分类到部分中。我已经阅读了许多教程,但是,大多数教程只使用部分和行的静态硬代码,看起来像这样:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0){
    return 1;
}
else if (section == 1){
    return 5;
} }

因为贷款项目数组是动态的,所以我不能硬编码行或部分来返回某些值。任何人都可以向我强调我应该如何继续,以便我可以返回动态值?如果可能,示例将不胜感激!谢谢大家:)

4

3 回答 3

1

在这种情况下,我编写了一个 NSOperation 按日期过滤项目(一个字典,其键是日期,每个值是一个项目数组)然后您将通过获取字典 allkeys 数组来获取部分,对于每个部分,您将获得行数组使用 objectForKey 和日期。

要进行过滤,您可以使用许多方法,例如使用 NSPredicate ...

当 NSOperation 完成并且代码收到字典准备就绪的通知时,您调用 reloadData 并且您可以根据需要组织表。

于 2012-07-25T08:12:54.757 回答
0

我假设您有一个可变字典数组,根据您的问题中显示的数据,例如:arrayOfMydata

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return 8;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
 {
     return [arrayOfMydata count];

 }


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

     NSDictionary *tempDictionary  =  [arrayOfMydata objectAtIndex:section];
     NSString *sectionString       =  [tempDictionary objectForKey:@"date"];
     return sectionString;
 }

希望对你有帮助

于 2012-07-25T08:25:26.753 回答
0

以下代码供您参考。这array是您发布的数组,mutDic是@Esses77 提到的字典

NSArray *array;

NSMutableArray *dateArr = [NSMutableArray array];

// dateArr is used to collect all the date in your array
for (NSDictionary *dic in array) {
    [dateArr addObject:[dic valueForKey:@"date"]];
}


//the following code is used to remove all the duplicate date
NSArray *copy = [dateArr copy];

NSInteger index = [copy count] - 1;

for (id object in [copy reverseObjectEnumerator])
{

    if ([dateArr indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound)

    {
        [dateArr removeObjectAtIndex:index];

    }

    index--;
}

//group all the dictionaries with the same date in mutDic. and key is the date, object is the array of dictionaries with the same date
NSMutableDictionary *mutDic = [NSMutableDictionary dictionary];

for (NSString *date in dateArr) {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date like %@", date];

    [mutDic setValue:[array filteredArrayUsingPredicate:predicate] forKey:date];

 }

您的部分数是 mutDic 中的键数,行数是相应的对象(它是一个数组).count。

于 2012-07-25T08:42:59.353 回答