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

标签 ios uitableview

我是 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/

相关文章:

ios - UITableView 部分标题的自动布局问题

ios - 零 sockaddr_in 在 SCNetworkReachabilityCreateWithAddress 中意味着什么?

ios - 如何使用拆分和映射方法?

ios - 使用 UIProgressView Indicator 在 UITableView 中下载图像

iphone - 如何将静态广告横幅添加到 UITableViewController 中?

ios - 转到不同 UITableViewCell 中的下一个 UITextField

ios - 无法将 UITableViewDataSource 与自定义单元格一起使用

ios - 使用在 UIImageView 上设置的启动图像

c# - MonoTouch 中导航栏的附加按钮

ios - 向 UIImagepickercontroller 导航栏添加后退按钮