2016-11-25 34 views
0

我是新來的iOS,動態部分和行的UITableView

我開發UITableView與動態行和部分

我想顯示在第一部分中的所有「今天」的數據,「明天」在其它區間和「星期日」的其他部分,等等...

碼我曾嘗試是,

這裏是我的數組:

{ 
     "callbackSlotId": "1234-1234-3214-1233", 
     "callbackSlotName": "10:00-10:30", 
     "day": "today", 
     "availablenow": true 
    }, 
    { 
     "callbackSlotId": "1234-1234-3214-1234", 
     "callbackSlotName": "11:00-11:30", 
     "day": "today", 
     "availablenow": false 
    }, 
    { 
     "callbackSlotId": "1234-1234-3214-1235", 
     "callbackSlotName": "10:30-11:00", 
     "day": "today", 
     "availablenow": false 
    }, 
    { 
     "callbackSlotId": "1234-1234-3214-1236", 
     "callbackSlotName": "10:30-11:00", 
     "day": "tomorrow", 
     "availablenow": false 
    }, 
    { 
     "callbackSlotId": "1234-1234-3214-1237", 
     "callbackSlotName": "10:30-11:00", 
     "day": "tomorrow", 
     "availablenow": false 
    }, 
{ 
    "callbackSlotId": "1234-1234-3214-1241", 
    "callbackSlotName": "10:30-11:00", 
    "day": "Sunday", 
    "availablenow": false 
} 
... 
... 
... 

使用此代碼會顯示在多個部分中的所有數據:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [Array count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [Array count]; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"]; 

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    cell.textLabel.text=[Array objectAtIndex:indexPath.row]; 

    return cell; 

} 

任何一個能幫助我嗎? 感謝閱讀

回答

0

未經測試,但你可以使用我的代碼的想法分離的陣列

NSMutableArray *sections = [[NSMutableArray alloc] init]; 
NSMutableArray *elementsInSection = [[NSMutableArray alloc] init]; 
for (NSDictionary *dict in Array) { 
    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]]; 
    } 
} 

現在你的tableview數據源是成爲

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [sections count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [elementsInSection count]; 
} 
+1

只需用'[elementsInSection addObject:arr]'替換'[elementsInSection addObject:day];'' –

+0

感謝它的工作,但最後懷疑我怎麼在'cellForRowAtIndexPath'中顯示名爲'callbackSlotName'的valueForKey –

+0

elementsInSection中的每個元素都是一個數組的NSDictionary,每個NSDictionary格式爲:「callbackSlotId」:「1234-1234-3214-1234」, 「callbackSlotName」:「11:00-11:30」, 「day」:「today」, 「所以你只需訪問 'NSDictionary * dict = [[elementsInSection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];' – nynohu

0

爲了解決這種情況下,可以先對day變量的值基礎分離Array成不同的子陣列。 爲此,您可以使用NSPredicate

因此,假設你主要Array有3級不同的值 - today, tomorrow, Sunday那麼你的陣列將有3子陣列

  • 數組[0]將包含daytoday所有對象

  • 陣列[1]將包含與day所有對象作爲tomorrow,等等

現在你可以使用它通過下面的代碼

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [Array count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [[Array objectAtIndex:section] count]; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"]; 

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    cell.textLabel.text=[[Array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 

    return cell; 

} 
+0

有沒有3個部分的部分是根據稱爲「日」我的鑰匙來決定的。e'today','tomorow'等 –

+0

我知道爲什麼寫了假設,你需要編寫一個邏輯來產生儘可能多的子數組,因爲你的日變量有不同的值。 –

0

它會適用於我請嘗試在cellForRowAtIndex路徑方法。

NSDictionary *dict = [_arrMytrips objectAtIndex:indexPath.row]; 
    NSString *day = [dict objectForKey:@"day"]; 
     if ([day isEqualToString:@"today"] && indexPath.section == 0) { 

      cell.textLabel.text = [dict objectForKey:@"day"]; 
      cell.detailTextLabel.text = [dict objectForKey:@"day"]; 

     }else{ 
      cell.textLabel.text = [dict objectForKey:@"day"]; 
      cell.detailTextLabel.text = [dict objectForKey:@"day"]; 
     } 

希望它對你有幫助。謝謝。

0

嘗試這樣:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    arrayMain = @[yourArray]; 
    arraySection = [NSMutableArray array]; 
    [arrayMain enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) { 

     int flag = 0; 
     for(NSDictionary *secObj in arraySection) { 
      if ([secObj[@"day"] isEqualToString:obj[@"day"]]) { 
       ++flag; 
       break; 
      } 
     } 

     if (flag == 0) { 
      [arraySection addObject:obj]; 
     } 
    }]; 
} 

然後:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return arraySection.count; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSMutableArray *arrayOfRows = [NSMutableArray array]; 

    [arrayMain enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) { 
     if ([obj[@"day"] isEqualToString:arraySection[section][@"day"]]) { 
      [arrayOfRows addObject:obj]; 
     } 
    }]; 

    return arrayOfRows.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    NSMutableArray *arrayOfRows = [NSMutableArray array]; 

    [arrayMain enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) { 
     if ([obj[@"day"] isEqualToString:arraySection[indexPath.section][@"day"]]) { 
      [arrayOfRows addObject:obj]; 
     } 
    }]; 

    NSDictionary *objectToShow = arrayOfRows[indexPath.row]; 

    cell.textLabel.text = objectToShow[@"callbackSlotName"]; 

    return cell; 
} 

通過這個代碼,您不必計算那些僅僅把它放到你的代碼,並相應地更改值。如果您有任何疑問,請隨時詢問。