2016-11-25 19 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; 
} 

通过这个代码,您不必计算那些仅仅把它放到你的代码,并相应地更改值。如果您有任何疑问,请随时询问。