2016-04-04 27 views
1

İçlerinde NSDate özelliğini içeren nesneler dizisi var. Ve onları UITableView'da başarıyla dolduruyorum.Nasıl Aya göre UITableView bölümlerini yapmak - iOS

Onların aya göre UITableView bölümlerde onları (nesneler) dağıtmak istiyorum.

Bunu nasıl yapabilirim?

+0

Gruba bunları tarafından gerek aylar sonra bölümlerin sayısını döndür. – tbilopavlovic

cevap

0

yapabilirsiniz grup kodu altında kullanarak nesneleri (örneğin kişi):

NSMutableArray * arrObjects = [NSMutableArray yeni]; aşağıdaki gibi çıkış verir

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 Eğer numberOfSections ve numberOfRows temsilci yöntemleri

kullanabileceğiniz size nesneler 12 ay bilge dizi dizisidir
2

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 :)