iOS动态表 - 将数据分类为行和节

时间:2012-07-25 08:04:43

标签: iphone ios dynamic uitableview

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

(
    {
    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;
} }

因为贷款项目数组是动态的,因此我无法对行或部分进行硬编码以返回特定值。任何人都可以向我强调我应该如何继续,以便我可以返回动态值?如果可能的话,我们将不胜感激!谢谢大家:))

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分: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;
 }

希望这会对你有所帮助

答案 2 :(得分: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。