ios - UitableView部分中的行数

标签 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];

}

/////////////////////单元格显示
    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

我希望它按日期分组(已完成),然后获取具有正确值的部分数据的正确性。谁能帮我这个??

最佳答案

从问题上还不清楚您面临的问题在哪里。
但是,如果要在单元格中显示数据,则可以应用以下方法。

接收到的数据:

(
  {
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"];

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

关于ios - UitableView部分中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21568584/

相关文章:

ios - 动态模拟 iOS 动态类型系统文本大小 (UIContentSizeCategory)

iphone - 如何复制 NSMutableArray

objective-c - NSMutableArray 中的 lastObject 是否返回对象的副本?

iphone - 我可以将 ObjectiveC @selector 放入 NSDictionary 中吗?

iOS NSDictionary 保存到 NSUserDefaults 不保存值

ios - 如何按下 tableview 单元格以在导航 Controller 中显示带有文本的 View Controller

ios - 在 for 循环中设置变量

iphone - 适用于 iPhone 的 Http Live Streaming 以及我们使用 .m3u8 文件的原因

ios - 无法将对象添加到单例中的 nsmutablearray

iphone - 获取 nsdictionary 中特定键的值