简体   繁体   English

填充分段的UITableView

[英]Populating a sectioned UITableView

Ok, this question has probably been asked a couple of times but I can't quite find an example that helps me getting forward. 好的,可能已经问过几次这个问题,但是我找不到一个可以帮助我前进的例子。

I'm trying to create a sectioned UITableView that acts as some sort of history: it should be populated by an NSMutableArray that consists of HistoryBatchVO objects. 我正在尝试创建一个分段的UITableView来充当某种历史记录:它应该由一个包含HistoryBatchVO对象的NSMutableArray填充。 Those HistoryBatchVO objects contain a timestamp (NSDate) and a names array (NSMutableArray) which in turn contains objects of type NameVO that contains (among other) a NSString. 这些HistoryBatchVO对象包含一个时间戳(NSDate)和一个名称数组(NSMutableArray),后者又包含NameVO类型的对象,该对象包含(其中包括)一个NSString。

I'd like to use the timestamp as the sections header and the strings from NameVOs to be filled into the sections in the table accordingly. 我想使用时间戳作为节标题,并将NameVO的字符串相应地填充到表的节中。

In my table controller I have: 在我的表控制器中,我有:

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

(Note: _dataModel.history is my NSMutableArray of HistoryBatchVOs). (注意:_dataModel.history是我的HistoryBatchVOs的NSMutableArray)。 ... but I suppose numberOfRowsInSection needs to return the number of objects in my HistoryBatchVO.names array. ...但是我想numberOfRowsInSection需要返回我的HistoryBatchVO.names数组中的对象数。 Question is how do I do that? 问题是我该怎么办?

Also, how do I change the implementation for cellForRowAtIndexPath to get this working? 另外,如何更改cellForRowAtIndexPath的实现以使其正常工作?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    HistoryBatchVO *batchVO = [_dataModel.history objectAtIndex:indexPath.row];

    return cell;
}

UPDATE: After solving the issue, Here's the working code: 更新:解决问题后,以下是工作代码:

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


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    HistoryBatchVO *h = [_dataModel.history objectAtIndex:section];
    return [h.names count];
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    HistoryBatchVO *h = [_dataModel.history objectAtIndex:section];
    return [NSString stringWithFormat:@"%@", h.timestamp];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"Cell";
    UITableViewCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    HistoryBatchVO *batchVO = [_dataModel.history objectAtIndex:indexPath.section];
    NameVO *nameVO = [batchVO.names objectAtIndex:indexPath.row];
    cell.textLabel.text = nameVO.string;

    return cell;
}

Assuming names is an array and with some more assumption of data types, the program skeleton will be something like this, but you have to accommodate with your needs. 假设names是一个数组,并且假设数据类型更多,程序框架将是这样,但是您必须满足您的需求。

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    HistoryBatchVO * h = [_dataModel.history objectAtIndex:section];
    return [h.names count];
}

You need also to implement the section title things, but I think you will like to format the dates using NSDateFormatter . 您还需要实现本节标题的内容,但我想您想使用NSDateFormatter格式化日期。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
     HistoryBatchVO * h = [_dataModel.history objectAtIndex:section];
     return h.date;
}

The cell element will be something like this, 单元格元素将是这样的,

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"Cell";
    UITableViewCell *cell = nil;       
    cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    if ( cell == nil)
       cell = [[UITableViewCell alloc] UITableViewCellStyle:UITableViewCellStylePlain reuseIdentifier:cellID];

    HistoryBatchVO *batchVO = [_dataModel.history objectAtIndex:indexPath.section];
    NameVO * nm = [batchVO.names objectAtIndex:indexPath.row];
    cell.textLabel.text = nm.name

    return cell;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM