简体   繁体   English

创建按日期划分的 UITableView

[英]Create a UITableView sectioned by Dates

How does one create a UITableView sectioned by Date?如何创建按日期划分的 UITableView? And how would the dates of the objects be used when the tableview is setup?设置表格视图时如何使用对象的日期?

If you already know dates that you want to use for sections store them in an array as arrays.如果您已经知道要用于部分的日期,请将它们存储在一个数组中,如 arrays。 So in another words create an array for every date you want to act as a section.因此,换句话说,为您想要充当部分的每个日期创建一个数组。 Then go through your customObjects and insert them into appropriate section array.然后 go 通过您的 customObjects 并将它们插入到适当的部分数组中。 When you have this use method numberOfSectionsInTableView to get the number of sections.当你有这个使用方法numberOfSectionsInTableView来获取节数时。

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

Then you will have to tell UITableDelegate how many rows you will need per section.然后你必须告诉 UITableDelegate 每个部分需要多少行。 To do that you use numberOfRowsInSection .为此,您使用numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[datesArray objectAtIndex:section] count];
}

Then in the method cellForRowAtIndexPath simply get customObject data for a cell from an appropriate section array.然后在cellForRowAtIndexPath方法中,只需从适当的节数组中获取单元格的 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;
}

You want to create a grouped UITableView with the date of each object as the sections of the table.您想要创建一个分组的 UITableView,其中每个 object 的日期作为表的部分。

You would likely want to read your dates into an array, storing only the unique dates, and use that array count to set the number of sections in your table.您可能希望将日期读入一个数组,仅存储唯一日期,并使用该数组计数来设置表中的节数。 Then match each date in the array with the objects in your data source that match that date when populating rows in each sections.然后,在填充每个部分的行时,将数组中的每个日期与数据源中与该日期匹配的对象进行匹配。 (Unless your data is already sorted in your datasource by date). (除非您的数据已按日期在数据源中排序)。

numberOfSectionsInTableView would be based on the number of distinct dates in your date numberOfRowsInSection would be based on the number of elements with each date numberOfSectionsInTableView 将基于您的日期中不同日期的数量 numberOfRowsInSection 将基于每个日期的元素数量

Use indexPath which would hold your section (Date) and row (objects) against your dictionary or other data source to get the data for the cellForRowAtIndexPath method.使用 indexPath 将您的部分(日期)和行(对象)保存在您的字典或其他数据源中,以获取 cellForRowAtIndexPath 方法的数据。

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

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