我是Objective-C的初学者,我在管理表时遇到了很多麻烦!在这种情况下,我有一个具有可变数量的部分的表,每个部分,我有一个可变数量的行。
在方法" getTimeslots"我得到一个代表约会列表的JSON。这是我的JSON的一小部分:
data{
"Sunday, August 10, 2014" = {
timeslots = (
{
id = 1013;
idPatient = "<null>"
status = 0;
timeEnd = 5:15 p.m.;
timestart = 5:00 p.m;
}
{
id = 1045;
idPatient = "<null>"
status = 0;
timeEnd = 5:30 p.m.;
timestart = 5:15 p.m.;
}
);
};
"Sunday, July 13, 2014" = {
timeslots = (
{
id = 705;
idPatient = "<null>"
status = 0;
timeEnd = 4:00 P.m;
timestart = 3:45 p.m.;
}
{
id = 725;
idPatient = "<null>"
status = 0;
timeEnd = 4:15 p.m.;
timestart = 4:00 p.m;
}
{
id = 745;
idPatient = "<null>"
status = 0;
timeEnd = 4:30 p.m
timestart = 4:15 p.m.;
}
{
id = 1009;
idPatient = "<null>"
status = 0;
timeEnd = 5:15 p.m.;
timestart = 5:00 p.m;
}
{
id = 1041;
idPatient = "<null>"
status = 0;
timeEnd = 5:30 p.m.;
timestart = 5:15 p.m.;
}
);
}
AND LOT OF DYNAMICS DAYS AND DYNAMICS APPOINTMENTS....
};
我找到了如何在我的表格中添加&#34; tableTimeslots&#34;每天一节,但我不知道如何添加与这些日子相关的约会。我尝试了很多东西,但什么也做不了:(
这是我的代码:
@interface AgendaViewController ()
{
NSMutableArray * sectionsArray;
NSMutableArray * rowsInSectionsArray;
}
@end
@implementation AgendaViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Title
self.title = @"Agenda";
sectionsArray = [[NSMutableArray alloc]init];
rowsInSectionsArray = [[NSMutableArray alloc]init];
[self getTimeslots];
[[self tableTimeslots]setDelegate:self];
[[self tableTimeslots]setDataSource:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [sectionsArray count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [rowsInSectionsArray count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [sectionsArray objectAtIndex:section];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Cellidentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifier];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier];
}
cell.textLabel.text = [rowsInSectionsArray objectAtIndex:indexPath.row];
return cell;
}
- (void)getTimeslots
{
NSURL *url = [NSURL URLWithString:getDataURL];
NSData *allData = [NSData dataWithContentsOfURL:url];
NSError *error;
id jsonObject = [NSJSONSerialization JSONObjectWithData:allData options:NSJSONReadingMutableContainers error:&error];
NSDictionary *data = [jsonObject objectForKey:@"data"];
for (int i = 0; i < data.count; i++)
{
NSArray *days = [data allKeys];
[sectionsArray addObject:days[i]];
NSArray *tsArray = [data objectForKey:[days objectAtIndex:i]];
NSArray *tsList = [tsArray valueForKey:@"timeslots"];
for (NSDictionary * oneTS in tsList)
{
NSString *TSid = [oneTS objectForKey:@"id"];
NSString *TSstatus = [oneTS objectForKey:@"status"];
NSString *TSstart = [oneTS objectForKey:@"timeStart"];
NSString *TSend = [oneTS objectForKey:@"timeEnd"];
NSString *TSidp = [oneTS objectForKey:@"idPatient"];
对于行,我试试这个但不行:
[rowsInSectionsArray addObject:TSid];
NSArray *newRow = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[rowsInSectionsArray count]-1 inSection:i]];
[[self tableTimeslots] insertRowsAtIndexPaths:newRow withRowAnimation:UITableViewRowAnimationTop];
}
}
[self.tableTimeslots reloadData];
}
@end
如果有人可以帮助我那会很棒,因为我迷路了!提前致谢
答案 0 :(得分:2)
您的tableview从数据源构建其部分和行。数据源表示tableview应该有[sectionsArray count]
行(有效)和[rowsInSectionsArray count]
行。
在getTimeslots
中,您将所有时间段添加到同一个数组中。这样,tableview认为所有部分应该具有相同的行数,但情况并非如此。每个部分应该有一个行数组,最好在另一个数组中:
rowsInSectionsArray = [[NSMutableArray alloc] init];
...
NSMutableArray *timeslotsInDay = [[NSMutableArray alloc] init];
for (NSDictionary * oneTS in tsList)
{
NSString *TSid = [oneTS objectForKey:@"id"];
[timeslotsInDay addObject:TSid];
}
[rowsInSectionsArray addObject:timeslotsInDay];
然后在-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
函数中执行
if (rowsInSectionsArray.count > section) {
return rowsInSectionsArray[section].count;
}