按标题(日期)对tableview中的单元格进行排序

时间:2018-03-08 18:59:34

标签: objective-c uitableview sorting date

如何按标题对tableview中的单元格进行排序(标题是dd / mm / yyyy格式的日期)?

我的代码:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"total item in table %lu",(unsigned long)self.items.count);
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"
                                                            forIndexPath:indexPath];
    [self configureCell:cell atIndex:indexPath];
    return cell;
}

- (void)configureCell:(UITableViewCell*)cell atIndex:(NSIndexPath*)indexPath {

            // Get current Item
            Item *item = self.items[indexPath.row];
            cell.textLabel.text = item.name; // HERE IS DATE in dd/mm/yyyy format
            cell.detailTextLabel.text = item.itemDetails.note;

            /*
            NSArray *productsOrdered = nil;
            productsOrdered = [self.items sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]];
             */
        }
}

我明白了:

simulator image

没有正确排序。 我需要列表中最新的日期和旧日期。

1 个答案:

答案 0 :(得分:0)

在调用self.items

之前,您需要排序[self.tableView reloadData];
相关问题