gpt4 book ai didi

ios - TableView 中的动态节数和行数

转载 作者:行者123 更新时间:2023-11-28 22:04:16 26 4
gpt4 key购买 nike

我是 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....
};

我找到了如何在我的表“tableTimeslots”中每天添加一个部分,但我找不到如何添加与这些天相关的约会。我尝试了很多东西,但什么也做不了:(

这是我的代码:

@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

如果有人能帮助我那会很棒,因为我迷路了!提前致谢

最佳答案

您的 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;
}

关于ios - TableView 中的动态节数和行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24396291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com