使用数组中的日期按日期对 UITableview 进行排序

Posted

技术标签:

【中文标题】使用数组中的日期按日期对 UITableview 进行排序【英文标题】:Using date from array to sort UITableview by date 【发布时间】:2013-04-22 18:22:05 【问题描述】:

我有一个包含以下信息的数组,从 JSON 中检索到:

Contents of events: (
    "<CalendarModel> \n   [title]: Arsenal v Man Utd\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-28T17:00:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Reading v QPR\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-28T14:30:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Newcastle v Liverpool\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-27T18:30:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: TBC\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-27T16:00:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Man City v West Ham\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-27T13:45:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Man Utd v Aston Villa\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-22T21:00:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Liverpool v Chelsea\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-21T17:00:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Spurs v Man City\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-21T14:30:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Fulham v Arsenal\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-20T16:00:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: West Ham v Man Utd\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-17T20:45:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Arsenal v Everton\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-16T20:45:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Chelsea v Man City\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-14T17:00:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Stoke v Man Utd\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-14T15:00:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Millwall v Wigan\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-13T18:15:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Reading v Liverpool\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-13T16:00:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: FC Basel v Spurs\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-11T21:05:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Rubin Kazan v Chelsea\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-11T21:05:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Juventus v Bayern Munich\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-10T20:45:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Barcelona v Paris St Germain\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-04-10T20:45:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Everton v Stoke\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-03-30T18:30:00.000+01:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Swansea v Spurs\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-03-30T16:00:00.000+01:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Sunderland v Man Utd\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-03-30T13:45:00.000+01:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Ireland v Austria\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-03-26T20:45:00.000+01:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Denmark v Bulgaria\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-03-26T20:15:00.000+01:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Sweden v Ireland\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-03-22T19:45:00.000+01:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Dennis McLaughlin\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-03-31T22:30:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Lee Thomas\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-03-30T23:00:00.000+01:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Lee Thomas\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-03-29T22:30:00.000+01:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Lee Thomas\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-03-28T21:30:00.000+01:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Lee Thomas\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-03-27T21:30:00.000+01:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Pat Kelly\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-02-14\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Ray Weaver\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-01-31\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Take the Biscuit\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-01-24\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: The McLaughlin Clan\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-01-18\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Local Artist\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-01-17\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Richie Vargas\n   [time]: (\n       \"<Time> \\n   [startTime]: 2013-01-10\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Tom Valbak\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-12-28\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Tom Brakl\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-12-25\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Tom Brakl\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-12-23\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Dessie Higgins\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-12-20\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Pat Kelly\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-12-13\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Rich Vargas\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-12-06\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Lee Thomas\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-11-22\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: International Jam Session\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-09-03T21:00:00.000+02:...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Kevin McKernan\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-08-30\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Lee Thomas\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-08-09\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Ray Weaver\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-07-19\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Gray Hawthorn\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-07-05\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Jimmy Walsh\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-06-07\\n   [endTime]: 2...\n</CalendarModel>",
    "<CalendarModel> \n   [title]: Take The Biscuit\n   [time]: (\n       \"<Time> \\n   [startTime]: 2012-11-29\\n   [endTime]: 2...\n</CalendarModel>"

我正在尝试使用 startTime(只有日期,而不是小时和分钟)作为 titleForHeaderInSection 创建表视图。包含事件的 NSMutableArray 称为 events

任何人都可以帮助我朝着正确的方向前进。就目前而言,我正在获取没有标题的数据,只有事件。我现在有以下内容,但我仍在测试。这至少会创建一个名为 Test 的标头

    #pragma mark - table methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return events.count;


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    return 1;


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

    CalendarModel* event = events[section];

    NSString *dato = [[event.time objectAtIndex:0] startTime];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSzzz";
    NSDate *gmtDate = [formatter dateFromString: dato];
    formatter.dateFormat = @"dd-MM-yyyy";
    dato = [formatter stringFromDate:gmtDate];
    return dato;


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


    CalendarModel* event = events[indexPath.row];


    NSString *dato = [[event.time objectAtIndex:0] startTime];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSzzz";
    NSDate *gmtDate = [formatter dateFromString: dato];
    formatter.dateFormat = @"HH:mm";
    dato = [formatter stringFromDate:gmtDate];


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SportCell" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",
                           event.title
                           ];
    cell.detailTextLabel.text = dato;


    return cell;

已编辑上述代码。它现在可以工作了,但每次都显示相同的游戏。好像它总是拿第一个,并表明了这一点。我认为问题出在

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    return 1;

方法。

【问题讨论】:

【参考方案1】:

你的 json 数据给我http://json.parser.online.fr/ 中的错误

试试这个:

   - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 


         CalendarModel* event = events[section];  // i think CalendarModel is a class in which you store yor json data one by one.
         return event.Title;  // In event class you have to save title;
       //return [[events objectAtIndex: section] objectForKey: @"Title"];
      

【讨论】:

那不是 json ,我认为他正在显示一个 NSManagedObjects 数组。【参考方案2】:

我认为您在属性 Title 中使用了大写字母。 在数组中它只是'title'

试试下面的代码行。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

  return [[events objectAtIndex: section] objectForKey: @"title"];

如果它不起作用,请告诉我们CalendarModel

您的实现的一个主要问题是,您的标题将始终显示第一个标题作为标题,因为您只有一个部分

如果我是 rite 你需要这样实现

#pragma mark - table methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return events.count;


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    return 1;


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

    return [[events objectAtIndex: section] objectForKey: @"title"];


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


    CalendarModel* event = events[indexPath.section];


    NSString *dato = [[event.time objectAtIndex:0] startTime];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSzzz";
    //formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:3600];
    //formatter.calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    //formatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_GB"];
    NSDate *gmtDate = [formatter dateFromString: dato];
    formatter.dateFormat = @"dd-MM-yyyy HH:mm";
    dato = [formatter stringFromDate:gmtDate];


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SportCell" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",
                           event.title
                           ];
    cell.detailTextLabel.text = dato;


    return cell;

【讨论】:

以上是关于使用数组中的日期按日期对 UITableview 进行排序的主要内容,如果未能解决你的问题,请参考以下文章

使用 NSSortDescriptor、Core Data 和 NSFetchedResultsController 按日期和名称对 UITableView 进行排序

按日期条件对 UItableView 部分进行排序

如何在uitableview的cellForRowAt函数中按标题日期对响应进行排序

如何对数组中的日期进行分组?

在 Swift 中向 UITableView 添加按日期分隔的部分

如何按日期对我的表格视图中的数组进行排序