0

我有一些从数据库中获取的 JSON 数据。我可以把它拉好并将其加载到我的表格视图中。我的问题是分离我的 JSON 数据,以便我可以对 tableview 进行分区。

JSON

[{"id":"1","name":"steve mans","phoneNumber":"(559)123-4455","showNumber":"1","greenCard":"1","expiration":"2014-02-15","driver":"1","paid":"1","watch":"1"},
{"id":"2","name":"myself and me","phoneNumber":"(559)321-6784","showNumber":"1","greenCard":"1","expiration":"2013-10-18","driver":"0","paid":"0","watch":"2"},
{"id":"4","name":"tod bellesmithson","phoneNumber":"(559)678-3421","showNumber":"0","greenCard":"1","expiration":"2013-11-22","driver":"1","paid":"0","watch":"2"},
{"id":"3","name":"John Smith","phoneNumber":"(559)123-1234","showNumber":"1","greenCard":"0","expiration":"2013-10-08","driver":"0","paid":"1","watch":"3"},
{"id":"5","name":"greg smith","phoneNumber":"559 345-1234","showNumber":"1","greenCard":"1","expiration":"2013-10-08","driver":"0","paid":"1","watch":"3"}]

我想要做的是,在我的表格视图中将这些数据分成三个部分。所以我想创建三个不同的表并将每个表加载到 tableview 的不同部分。但是每个人的信息都是相同的(身份证、姓名、电话等)。所以我最终得到了一张桌子,并添加了一个列,指定人们工作的班次,“观看”。那么如何使用监视列分隔数据,所以在我的表格视图中我将拥有:

第一节 夜班 第二节 早班 第三节 夜班

4

3 回答 3

1

试试这个代码:

NSArray *data = (NSArray)[NSJSONSerialization JSONObjectWithData:jsonData 
                                             options:NSJSONReadingMutableContainers 
                                               error:&error];

NSMutableArray *morningShift = [NSMutableArray array];
NSMutableArray *noonShift = [NSMutableArray array];
NSMutableArray *nightShift = [NSMutableArray array];


for (int i=0; i< [data count]; i++)
{
   NSDictionary *item = data[i];

   if (@"1" == item[@"watch"] )
  {
    [morningShift addObject:item];
  } else if (@"2" == item[@"watch"] )
  {
    [noonShift addObject:item];
  } else if (@"3" == item[@"watch"] )
  {
    [nightShift addObject:item];
  } 
}
于 2013-10-26T04:08:58.457 回答
1

试试这个

    NSMutableArray *tableData = [NSMutableArray alloc] init];
    NSArray* nameArr = [NSArray arrayWithObjects: @"1", @"2",@"3",nil];
    for(int i=0; i<[nameArr count]; i++)
    {
         [tableData addObject:[jsonArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"watch = %@",[nameArr objectAtIndex:i]]] ];
    }

TableView 委托方法

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

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

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

 {
      UITableViewCell *cell= nil;
     //implement your logic to display the cell item;
     NSArray sectionData = [tableData objectAtIndex:indexPath.section];
     NSArray rowData = [sectionData objectAtIndex:indexPath.row];       

     return cell;   
 }

请注意我没有编译代码。有编译错误的可能。

于 2013-10-26T04:29:02.867 回答
1

检查 cellForRowAtIndexPath 方法中的部分并相应地加载数据,

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

{

//initialize cell

if (indexPath.section == 0){

// load data
}

返回单元格;
}

于 2013-10-26T06:25:03.437 回答