【问题标题】:iOS : sort information base on date Modified on UITableViewiOS:根据 UITableView 上修改的日期对信息进行排序
【发布时间】:2014-07-25 19:41:37
【问题描述】:

我有一个 UITableView,我从服务器获取信息,我想根据修改日期对我的信息进行排序,请你帮我完成这个实现,我该如何排序?

提前致谢!感谢答案部分的任何代码!

我从 json 修改的日期是这样的

 "dateModified": "2014-06-23T07:51:08.373Z"

我的 ViewDidLoad

- (void)viewDidLoad
{
[super viewDidLoad];

_mapView.showsUserLocation = YES;
_mapView.delegate = self;

[ApiManager fetchCoordinates:^(id result) {
    newAnnotations = [NSMutableArray array];
    CLLocationCoordinate2D location;
    NSArray *array=(NSArray*)result;

    for (NSDictionary *dictionary in array)
    {

        MyAnnotation *newAnnotation;

        newAnnotation = [[MyAnnotation alloc] init];
        newAnnotation.company = dictionary[@"company"];
        newAnnotation.dateModified = dictionary[@"dateModified"];


        [newAnnotations addObject:newAnnotation];
    }
    [self.mapView addAnnotations:newAnnotations];

} failure:^(NSError *error) {

}];
}

我的表格视图控制器

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: 
(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"Cell";

UITableViewCell *cell = [tableView 
dequeueReusableCellWithIdentifier:simpleTableIdentifier];

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
reuseIdentifier:simpleTableIdentifier];


MyAnnotation *newAnnotation=[newAnnotations objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@",newAnnotation.company];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",newAnnotation.dateModified];


return cell;
} 

【问题讨论】:

    标签: ios objective-c uitableview sorting nsmutablearray


    【解决方案1】:

    您必须对数组进行排序。试试这样的:

     self.sortedArray = [newAnnotations sortedArrayUsingComparator: ^(id obj1, id obj2) {
     MyAnnotation* ann1 = (MyAnnotation*)obj1;
     MyAnnotation* ann2 = (MyAnnotation*)obj2;
    
     NSComparisonResult result = [ann1.dateModified compare:ann2.dateModified]
     return result;
    

    }];

    其中 sortedArray 是私有属性:

    @property(strong, nonatomic) NSArray *sortedArray;
    

    另外,在cellForRowAtIndexPath: 中,仅当您无法将它们出列时才分配新单元格。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
    {
    
        static NSString *simpleTableIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
        }
    
    
        MyAnnotation *newAnnotation=[self.sortedArray objectAtIndex:indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@",newAnnotation.company];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",newAnnotation.dateModified];
    
    
        return cell;
    } 
    

    请注意,我正在进行的日期比较实际上是一个字符串比较,它之所以有效,只是因为您的日期格式为 YYYY-MM-DD。

    请注意我没有测试这段代码,所以把它当作一个大方向,不要只是复制粘贴。

    【讨论】:

    • 感谢您的评论我应该把第一部分放在哪里,我的意思是排序数组?
    • 你可以把它放在viewDidLoad中,例如
    • 抱歉这个问题,但是如何使用 sortedArray?
    • 哦,是的。我在答案中编辑了 cellForRowAtIndexPath 的代码。您只需使用 sortedArray 而不是 newAnntations 数组。为了能够从该方法访问 sortedArray,您可以将其设为类的私有属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多