Uitableview部分中的行数

时间:2014-02-05 04:21:03

标签: ios nsmutablearray nsdictionary uitableview

我有这样的数据:

(
      {
    code = 000932;
    date = "2013-11-29 18:17:03";
},
    {
    code = 000933;
    date = "2013-11-29 18:17:03";
},
    {
    code = 000934;
    date = "2013-11-29 18:17:03";
},
    {
    code = 000935;
    date = "2013-11-29 18:17:03";
},
    {
    code = 000936;
    date = "2013-11-29 18:17:03";
},
    {
    code = 000937;
    date = "2013-11-29 18:17:03";
},
    {
    code = 000938;
    date = "2013-11-29 18:17:03";
},
    {
    code = 000939;
    date = "2013-11-29 18:17:03";
},
    {
    code = 000940;
    date = "2013-11-29 18:17:03";
},
    {
    code = 001004;
    date = "2013-12-24 01:27:34";
},
    {
    code = 001005;
    date = "2013-12-24 01:27:34";
},
    {
    code = 001006;
    date = "2013-12-24 01:27:35";
},
    {
    code = 001007;
    date = "2013-12-24 01:33:17";
},
    {
    code = 001008;
    date = "2013-12-24 01:33:17";
},
    {
    code = 001009;
    date = "2013-12-24 01:33:17";
}
 )

然后我对它进行排序并将其“重新组合”为NSMutableDictionary

-(void)fetchCoupon{
    NSLog(@"3");
    userCoupon *object;
    //[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    NSString *url=[NSString stringWithFormat:@"%@/transaction/coupon/list",serverUrl];
    NSDictionary *parameters = @{
                                 @"token":[bbox_helper getToken],
                                 };
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        BOOL found;
        NSLog(@"data->%@",[responseObject objectForKey:@"data"]);
        for(NSDictionary *news_objects in [responseObject objectForKey:@"data"]){
            userCoupon *object=[[userCoupon alloc] initWithDictionary:news_objects];

            if(![coupon containsObject:object]){
                [coupon addObject:object];
            }
        }


        for (object in coupon)
        {
            NSString *c = object.date;


            found = NO;

            for (NSString *str in [coupon_section allKeys])
            {
                if ([str isEqualToString:c])
                {
                    found = YES;
                }
            }
            if (!found)
            {
                [coupon_section setValue:[[NSMutableArray alloc] init] forKey:c];
            }

        }

        // Loop again and sort the books into their respective keys
        for (object in coupon)
        {

            [[coupon_section objectForKey:object.date] addObject:object];

        }


        NSLog(@"data---%@",[[coupon_section allKeys] sortedArrayUsingComparator:^(id a, id b) {
            return [a compare:b options:NSNumericSearch];
        }]);

        NSLog(@"%lu",(unsigned long)[[[coupon_section allKeys] sortedArrayUsingComparator:^(id a, id b) {
            return [a compare:b options:NSNumericSearch];
        }]count]);
        progressView.mode = MRProgressOverlayViewModeCheckmark;

        progressView.titleLabelText = @"Done";

        [table_transaction reloadData];

        [table_gift reloadData];

        [table_coupon reloadData];

        [beetlebox performBlock:^{

            [progressView dismiss:YES];

        } afterDelay:2.0];


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        progressView.mode = MRProgressOverlayViewModeCross;

        progressView.titleLabelText = @"Failed communicating with server";


    }];

}

然后在tableviewsection中我创建了这样的

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return [[[[[coupon_section allKeys] sortedArrayUsingComparator:^(id a, id b) {
            return [a compare:b options:NSNumericSearch];
        }] reverseObjectEnumerator]allObjects] count];
    }

}

然后在标题标题部分

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

        return [[[[[coupon_section allKeys] sortedArrayUsingComparator:^(id a, id b) {
            return [a compare:b options:NSNumericSearch];
        }] reverseObjectEnumerator]allObjects]objectAtIndex:sections];;

    }

}

然后是行数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sections {

        return  [[[coupon_section allKeys] sortedArrayUsingComparator:^(id a, id b) {
            return [a compare:b options:NSNumericSearch];
        }]count];

}

///////////////////// cell show

    userCoupon *obj = [coupon objectAtIndex:indexPath.row];
    static NSString* cells=@"getGift";
    pointCell *cell = [[pointCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cells];

    if(!cell){
        cell = [[pointCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cells];
    }
    NSLog(@"data->%@",obj.code);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.merchant_name.text = obj.code;
    cell.status.hidden = YES;
    cell.icon_status.hidden = YES;
    return cell;

结果是:https://i.cloudup.com/V3wW9w4glP.png

我希望按日期分组(已完成),然后在具有正确值的部分上获取数据。任何人都可以帮我这个吗?

3 个答案:

答案 0 :(得分:2)

问题仍然不明确,问题在于您面临的问题。 但是,如果要在单元格中显示数据,可以应用以下方法。
已收到数据:

(
  {
code = 000932;
date = "2013-11-29 18:17:03";    

},
   {
code = 000933;
date = "2013-11-29 18:17:03";
}
)

如果你有排序的数组名为coupon ,那么你可以在 cellForRowAtIndexPath 中设置元素,如下所示:

cell.name.text = [[coupon objectAtIndex:indexpath.row]objectForKey:@"code"];

OR

cell.name.text =[[coupon objectAtIndex:indexpath.row]objectForKey:@"date"];

答案 1 :(得分:1)

试试这个

cellForRowAtIndexPath:

中的

NSArray *couponsArray = [[[coupon_section allKeys] sortedArrayUsingComparator:^(id a, id b) {
        return [a compare:b options:NSNumericSearch];
    }] objectAtIndex:indexPath.section];
userCoupon *obj = [couponsArray objectAtIndex:indexPath.row];

答案 2 :(得分:0)

JSON sample project中的TLIndexPathTools几乎完全符合您的要求。

特别是,TLIndexPathDataModel类会将数据组织成各个部分,并使用[dataModel numberOfRowsInSection:][dataModel itemAtIndexPath:]等API大大简化数据源和委托方法。