Lütfen bu kodu bir kez gözden geçirin.
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]);
}
[kendini createHederAndCellArray]; ----> Bu yöntem dizinizde veri ekledikten sonra çağırmanız ve aşağıdaki gibi tablo görünümünde kodu belirlemeniz gerekir.
#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;
}
Tarihe göre sıralamak için sözlük tuşlarını sıralamanız gerekiyor. Eğer [öz sortKeyAccordingTodate] kullanmaya gerek daha sıralama yöntemini kullanırsanız
-(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;
}
yerine [dictArrData AllKeys]
Umut bu size yardımcı olacaktır :)
Gruba bunları tarafından gerek aylar sonra bölümlerin sayısını döndür. – tbilopavlovic