2016-12-20 18 views
0

私はtimeSlotArrに与えられた形式のデータを含んでいます。複数の行とセクションを持つUITableview

 timeSlotArr ++++++++ (
     { 
     availablenow = 0; 
     callbackSlotId = "96a8a387-a2a1-e611-8101-000c29821561"; 
     callbackSlotName = "07:00 AM"; 
     date = "2016-12-21"; 
     dateInUtc = "2016-12-21T01:30:00Z"; 
     day = Wednesday; 
    }, 
     { 
     availablenow = 0; 
     callbackSlotId = "9aa8a387-a2a1-e611-8101-000c29821561"; 
     callbackSlotName = "09:00 AM"; 
     date = "2016-12-21"; 
     dateInUtc = "2016-12-21T03:30:00Z"; 
     day = Wednesday; 
    }, 
     { 
     availablenow = 0; 
     callbackSlotId = "9ca8a387-a2a1-e611-8101-000c29821561"; 
     callbackSlotName = "10:00 AM"; 
     date = "2016-12-21"; 
     dateInUtc = "2016-12-21T04:30:00Z"; 
     day = Wednesday; 
    } 
) 

データをUITableViewに複数のセクションと複数の行を表示したいとします。セクションの

何が「日」キー(sectionsが配列である)に基づいて決定されていないと、あなたのテーブルビューに上記のセクションと行構造を実現することができ、私の経験を1としてelementsInSectionはセクション

for (NSDictionary *dict in timeSlotArr) { 
    NSString *day = [dict objectForKey:@"day"]; 
    if (![sections containsObject:day]) { 
     [sections addObject:day]; 

     NSMutableArray *arr = [[NSMutableArray alloc] init]; 
     [arr addObject:dict]; 

     [elementsInSection addObject:arr]; 
    } else { 
     NSMutableArray *arr = [elementsInSection objectAtIndex:[sections indexOfObject:day]]; 
     [arr addObject:dict]; 

     [elementsInSection setObject:arr atIndexedSubscript:[sections indexOfObject:day]]; 
    } 
} 

NSLog(@"elementsInSection ++++++++ %@",elementsInSection); 
elementsInSection Returns 1 instead of 3 here is wrong... 3 means data of `timeSlotArr` with key `day` 

NSLog(@"sections ++++++++ %@",sections); //Returns 1 which is right 
+0

この行を削除して試してみてください:[elementsInSection setObject:arr atIndexedSubscript:[sections indexOfObject:day]]; –

+0

がどのように私はあなたがすでにその配列のリファレンスを使用しているelementsInSection' –

+0

'に要素を追加するNSMutableArrayの* ARR = [elementsInSection objectAtIndex:[セクションindexOfObject:1日]];要素を追加する。 –

答えて

1

内部のデータであり、

ステップ1:セクションで必要なものはDayです。 あなたの配列からすべてのユニークな日数を取得します。

NSArray *uniqueDays = [arrayData valueForKeyPath:@"@distinctUnionOfObjects.day"]; 

ユニーク日結果:

(
    Wednesday, 
    Monday, 
    Tuesday 
) 

ステップ2:

NSMutableDictionary *dictData1 = [NSMutableDictionary dictionary]; 

for (NSString *strDay in uniqueDays) { 
    NSArray *filteredDay = [arrayData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(day == %@)", strDay]]; 
    [dictData1 setObject:filteredDay forKey:strDay]; 
} 

フィルタリングされたデータの結果:今、私たちは一日賢明な配列のみ、その日のデータ配列が含まれている必要があります

{ 
    Monday =  (
       { 
      availablenow = 0; 
      callbackSlotId = "9ca8a387-a2a1-e611-8101-000c29821561"; 
      callbackSlotName = "10:00 AM"; 
      date = "2016-12-21"; 
      dateInUtc = "2016-12-21T04:30:00Z"; 
      day = Monday; 
     }, 
       { 
      availablenow = 0; 
      callbackSlotId = "9ca8a387-a2a1-e611-8101-000c29821561"; 
      callbackSlotName = "10:00 AM"; 
      date = "2016-12-21"; 
      dateInUtc = "2016-12-21T04:30:00Z"; 
      day = Monday; 
     } 
    ); 
    Tuesday =  (
       { 
      availablenow = 0; 
      callbackSlotId = "9ca8a387-a2a1-e611-8101-000c29821561"; 
      callbackSlotName = "10:00 AM"; 
      date = "2016-12-21"; 
      dateInUtc = "2016-12-21T04:30:00Z"; 
      day = Tuesday; 
     } 
    ); 
    Wednesday =  (
       { 
      availablenow = 0; 
      callbackSlotId = "96a8a387-a2a1-e611-8101-000c29821561"; 
      callbackSlotName = "07:00 AM"; 
      date = "2016-12-21"; 
      dateInUtc = "2016-12-21T01:30:00Z"; 
      day = Wednesday; 
     }, 
       { 
      availablenow = 0; 
      callbackSlotId = "9aa8a387-a2a1-e611-8101-000c29821561"; 
      callbackSlotName = "09:00 AM"; 
      date = "2016-12-21"; 
      dateInUtc = "2016-12-21T03:30:00Z"; 
      day = Wednesday; 
     }, 
       { 
      availablenow = 0; 
      callbackSlotId = "9ca8a387-a2a1-e611-8101-000c29821561"; 
      callbackSlotName = "10:00 AM"; 
      date = "2016-12-21"; 
      dateInUtc = "2016-12-21T04:30:00Z"; 
      day = Wednesday; 
     } 
    ); 
} 

ユニークな日数sectionarrayとして使用できるようになりました。

およびdictData辞書を行配列として使用し、セクションに基づいてデータをフェッチします。

編集:今、あなたが他の任意のものを、必要であれば、私に教えてください

#pragma mark --- UITableView DataSource methods 

//Section 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return uniqueDays.count; 
} 

//Row 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [[dictData1 objectForKey:uniqueDays[section]] count]; 
} 

//Cell 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

    NSArray *arrDayData = [dictData1 objectForKey:uniqueDays [indexPath.section]]; 
    NSDictionary *dictDetail = arrDayData[indexPath.row]; 

    aCell.textLabel.text = dictDetail[@"callbackSlotId"]; 

    return aCell; 
} 

テーブルビュー方法でフィルタアレイの上に使用しています。

+0

の 'numberOfRowsInSection'と' cellForRowAtIndexPath'の表示方法を教えてください –

+0

は 'numberOfRowsInSection'でday配列の数を渡すだけで、' cellForRowAtIndexPath'でも使用できます。 – CodeChanger

+0

最新の答えを確認してください。 'section'、' row'& 'cell'を表示する方法を教えてください。 – CodeChanger

関連する問題