创建一个按日期划分的UITableView

问题描述

| 如何创建按日期划分的UITableView?设置表视图时如何使用对象的日期?     

解决方法

如果您已经知道要用于节的日期,则将它们作为数组存储在数组中。因此,换句话说,为您要用作节的每个日期创建一个数组。然后遍历customObjects并将它们插入适当的section数组。当您具有此方法时,请使用方法“ 0”来获取节数。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [datesArray count];
}
然后,您将不得不告诉UITableDelegate每节需要多少行。为此,您使用
numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[datesArray objectAtIndex:section] count];
}
然后,在方法“ 4”中,只需从适当的节数组中获取单元格的customObject数据即可。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @\"CustomTableCell\";
    static NSString *CellNib = @\"UserCustomTableCell\";

    UserCustomTableCell *cell = (UserCustomTableCell *)[table dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (UserCustomTableCell *)[nib objectAtIndex:0];
    }

    MyObject *customObject = [[datesArray objectAtIndex:indexPath.section] indexPath.row];

    //Setup your cell here
    cell.date.text = [customObject date];

    return cell;
}
    ,您要创建一个分组的UITableView,将每个对象的日期作为表的各个部分。 您可能希望将日期读入数组,仅存储唯一的日期,然后使用该数组计数来设置表中的节数。然后,在每个部分中填充行时,将数组中的每个日期与数据源中与该日期匹配的对象进行匹配。 (除非您的数据已按日期在数据源中排序)。 numberOfSectionsInTableView将基于日期中不同日期的数量 numberOfRowsInSection将基于每个日期的元素数量 使用indexPath可使您的节(日期)和行(对象)相对于字典或其他数据源,以获取cellForRowAtIndexPath方法的数据。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...