我有一个包含NSDate
属性的对象数组。我正在UITableView
成功填充它们。
我想根据月份在UITableView
个部分中分发它们(对象)。
我该怎么做?
答案 0 :(得分:1)
请查看此代码一次。它可能对您有帮助。
NSMutableDictionary * dictArrData; //declare dictionary global
-(void)createHederAndCellArray
{
dictArrData=[[NSMutableDictionary alloc]init];
for(int i=0;i<arrHistory.count;i++)
{
yourObject *pastorders=[arrHistory objectAtIndex:i];
if([dictArrData objectForKey:pastorders.paymentDate])
{
NSMutableArray *arrTemp=[dictArrData objectForKey:pastorders.paymentDate];
[arrTemp addObject:pastorders];
[dictArrData setObject: arrTemp forKey:pastorders.paymentDate];
}
else
{
NSMutableArray * arrTemp = [[NSMutableArray alloc]init];
[arrTemp addObject:pastorders];
[dictArrData setObject: arrTemp forKey:pastorders.paymentDate];
}
}
NSLog(@“check dictionary %@",[dictArrData description]);
}
[self createHederAndCellArray]; ----&gt;在数组中添加数据后需要调用此方法,而不是在tableview中设置代码,如下所示。
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [dictArrData allKeys].count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *allValues=(NSArray*)[dictArrData objectForKey:[[dictArrData allKeys] objectAtIndex:section]];
return [allValues count];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 22, tableView.frame.size.width,15)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,25)];
[label setFont:[UIFont systemFontOfSize:14]];
label.textAlignment=NSTextAlignmentLeft;
label.textColor=[UIColor whiteColor];
NSString *sectionTitle=[NSString stringWithFormat:@"%@" ,[[dictArrData allKeys] objectAtIndex:section]];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date = [dateFormatter dateFromString:sectionTitle];
dateFormatter.dateFormat = @“MMM";
NSString *sectionTitle =[NSString stringWithFormat:@" %@", [dateFormatter stringFromDate:date]];
[view addSubview:imgvw];
[view addSubview:label];
[label setText:sectionTitle];
return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
yourcell *cell = [tableView dequeueReusableCellWithIdentifier:@“yourcellidentifier” forIndexPath:indexPath];
if (cell == nil)
{
cell = [[yourcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"yourcellidentifier"];
}
@try
{
//keyArr=[self sortKeyAccordingTodate];
NSArray *allValues=[dictArrData objectForKey:[[dictArrData allKeys] objectAtIndex:indexPath.section]];
if(allValues.count >0 )
{
yourObject *pastorder=[allValues objectAtIndex:indexPath.row];
cell.lblProductname.text=pastorder.itemName;
}
}
@catch (NSException *exception) {
NSLog(@"Exception :%@",exception.description);
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 25;
}
根据日期排序,而不是排序字典键。
-(NSArray *)sortKeyAccordingTodate
{
NSArray* sorted = [[dictArrData allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date = [dateFormatter dateFromString:obj1];
NSDate *date1 = [dateFormatter dateFromString:obj2];
if (date > date1) {
return (NSComparisonResult)NSOrderedAscending;
}
if (date < date1) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
return sorted;
}
如果你使用排序方法而不是你需要使用 [self sortKeyAccordingTodate] 而不是 [dictArrData allKeys]
希望这会对你有所帮助:)。
答案 1 :(得分:0)
您可以使用以下代码对对象(例如Person)进行分组:
NSMutableArray * arrObjects = [NSMutableArray new];
for (int i=0;i<12;i++) {
Person* p1 = [Person new];
p1.name = [NSString stringWithFormat:@"ABC %d", i+1];
p1.date = [[NSDate date] dateByAddingTimeInterval:60*60*24*30*i];
[arrObjects addObject:p1];
}
// add blank arrays for 12 months
NSMutableArray* matrixObjects = [NSMutableArray new];
for (int i=0; i<12; i++) {
[matrixObjects addObject:[NSMutableArray new]];
}
NSCalendar* calendar = [NSCalendar currentCalendar];
for (Person* p in arrObjects) {
int month = (int) [[calendar components:NSCalendarUnitMonth fromDate:p.date] month];
[matrixObjects[month-1] addObject:p];
}
// print resutls
for (int i=0; i<matrixObjects.count; i++) {
NSLog(@"Objects in Section %d", i);
for (Person* p in matrixObjects[i]) {
NSLog(@" ROW: %@ %@", p.name, p.date.description);
}
}
其中输出如下:
Objects in Section 0
ROW: ABC 11 2017-01-29 11:55:49 +0000
Objects in Section 1
ROW: ABC 12 2017-02-28 11:55:49 +0000
Objects in Section 2
Objects in Section 3
ROW: ABC 1 2016-04-04 11:55:49 +0000
Objects in Section 4
ROW: ABC 2 2016-05-04 11:55:49 +0000
Objects in Section 5
ROW: ABC 3 2016-06-03 11:55:49 +0000
Objects in Section 6
ROW: ABC 4 2016-07-03 11:55:49 +0000
Objects in Section 7
ROW: ABC 5 2016-08-02 11:55:49 +0000
Objects in Section 8
ROW: ABC 6 2016-09-01 11:55:49 +0000
Objects in Section 9
ROW: ABC 7 2016-10-01 11:55:49 +0000
ROW: ABC 8 2016-10-31 11:55:49 +0000
Objects in Section 10
ROW: ABC 9 2016-11-30 11:55:49 +0000
Objects in Section 11
ROW: ABC 10 2016-12-30 11:55:49 +0000
matrixObjects 是12个月对象数组的数组,您可以在 numberOfSections 和 numberOfRows 委托方法中使用