[英]UITableView with set number of sections, variable number of row per section
[英]Uitableview number of row in section
我有這樣的數據:
(
{
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"];
嘗試這個
在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];
TLIndexPathTools中的JSON示例項目幾乎完全符合您的要求。
特別是, TLIndexPathDataModel
類為您組織數據,並使用[dataModel numberOfRowsInSection:]
和[dataModel itemAtIndexPath:]
等API大大簡化數據源和委托方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.