Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios tableview中的列数据排序_Ios_Sorting_Nsarray_Nssortdescriptor - Fatal编程技术网

Ios tableview中的列数据排序

Ios tableview中的列数据排序,ios,sorting,nsarray,nssortdescriptor,Ios,Sorting,Nsarray,Nssortdescriptor,我使用的是表格视图,看起来像excel表格类型,可以说是多列。在列标题上添加平移手势以增加列的宽度,如excel工作表中所示。我在对列数据进行排序时遇到问题。列数据始终按字母/字符串格式排序。虽然我有一些数字类型列以及日期类型。因此,所有列都按字符串排序。 你能建议一些实现这一目标的方案吗。我将所有数据作为来自web服务的字典保存在一个数组中 让我分享一些排序代码 -(void)sortTableDataUsingSortKey:(NSString *)sortKey withAscending

我使用的是表格视图,看起来像excel表格类型,可以说是多列。在列标题上添加平移手势以增加列的宽度,如excel工作表中所示。我在对列数据进行排序时遇到问题。列数据始终按字母/字符串格式排序。虽然我有一些数字类型列以及日期类型。因此,所有列都按字符串排序。 你能建议一些实现这一目标的方案吗。我将所有数据作为来自web服务的字典保存在一个数组中

让我分享一些排序代码

-(void)sortTableDataUsingSortKey:(NSString *)sortKey withAscending:(BOOL)isAscending {

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:isAscending selector:@selector(compare:)];

    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    NSArray *arrTemp = [[NSArray alloc] initWithArray:[data_array sortedArrayUsingDescriptors:sortDescriptors]];
    [data_array count]?[data_array removeAllObjects]:NSLog(@"Datsource Table Not null");
    [data_array addObjectsFromArray:arrTemp];
    arrTemp = nil;

    [_table_view reloadData];
}

我解决了这个问题。我只是根据字典的数据类型添加了条件排序描述符方法。和在数据数组字典中添加数据的位置。以适当的数据类型添加数据。例如,如果数据是数字,则将其作为数字/浮点数或NSDate添加到字典中

现在,我根据排序列检查字典中的数据类型。并更改排序选择器(比较非字符串数据和使用不区分大小写的字符串数据:),rest工作正常

最后一种有效的方法是

-(void)sortTableDataUsingSortKey:(NSString *)sortKey withAscending:(BOOL)isAscending {
    NSSortDescriptor *sortDescriptor = nil;

    if ([data_array count] && [[[data_array objectAtIndex:0] valueForKey:sortKey] isKindOfClass:[NSString class]]) {
            sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:isAscending selector:@selector(caseInsensitiveCompare:)];
        }
        else {
            sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:isAscending selector:@selector(compare:)];
        }

    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    NSArray *arrTemp = [[NSArray alloc] initWithArray:[data_array sortedArrayUsingDescriptors:sortDescriptors]];
    [data_array count]?[data_array removeAllObjects]:NSLog(@"Datsource Table Not null");
    [data_array addObjectsFromArray:arrTemp];
    arrTemp = nil;

    [_table_view reloadData];
}

请为不同的数据类型添加一些使用
sortKey
参数的示例。我解决了这个问题。我只是根据字典的数据类型添加了条件排序描述符方法。并且在数据数组字典中添加了正确数据类型的数据。if([data_array count]&&&[[data_array objectAtIndex:0]valueForKey:sortKey]isKindOfClass:[NSString class]]){sortDescriptor=[[NSSortDescriptor alloc]initWithKey:sortKey升序:isAscending选择器:@selector(CaseSensitiveCompare:)]}else{sortDescriptor=[[NSSortDescriptor alloc]initWithKey:sortKey ascending:isAscending selector:@selector(compare:)];}很高兴听到这个消息,请将其作为答案写下来并接受它。