【问题标题】:NSSortDescriptor doesn't update UITableViewNSSortDescriptor 不更新 UITableView
【发布时间】:2015-11-08 21:20:36
【问题描述】:

我正在尝试在 UISegmentedControl 中使用 NSSortDescriptor 对我的 tableview 进行排序。当我记录数组进行排序时,它显示正确的排序顺序,但调用 [self.tableView reloadData] 后 tableview 没有更新;

数据来自一个由 json 提要填充的数组。我没有使用 NSObjects 来显示 tableview,它都是从 NSArray 填充的。见以下代码:

@interface LinksTableViewController (){

    NSArray *data;

}

@property (strong, nonatomic) NSArray *links;
@property (strong, nonatomic) NSArray *tNames;
@property (strong, nonatomic) NSArray *dThor;
@property (strong, nonatomic) NSArray *theLinker;
@property (strong, nonatomic) NSArray *anText;
@property (strong, nonatomic) NSArray *noDo;

@end

@implementation LinksTableViewController



- (void)viewDidLoad {
    [super viewDidLoad];

    UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"STAT1", @"STAT2", @"STAT3", @"STAT4", nil]];
    [statFilter sizeToFit];
    [statFilter addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
    self.navigationItem.titleView = statFilter;

    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(refreshdelay:) userInfo:nil repeats:NO];

}

- (void)MySegmentControlAction:(UISegmentedControl *)segment
{
    NSArray *arrayToSort = data;

    if (segment.selectedSegmentIndex == 0)
    {

        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"pda" ascending:NO];
        arrayToSort = [arrayToSort sortedArrayUsingDescriptors:@[sortDescriptor]];

        NSLog(@"%@", arrayToSort);

    }
    else if (segment.selectedSegmentIndex == 1)
    {


    }
    else if (segment.selectedSegmentIndex == 2)
    {

    }
    else if (segment.selectedSegmentIndex == 3)
    {

    }

    [self.tableView reloadData];
}

-(void)refreshdelay:(NSTimer*)timer
{

    NSString *myString = [links absoluteString];
    NSURL *JSONData = [NSURL URLWithString:myString];
    NSData *datas = [NSData dataWithContentsOfURL:JSONData];
    NSURLRequest *request = [NSURLRequest requestWithURL:JSONData];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    operation.responseSerializer.acceptableContentTypes = [operation.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSArray *jsonResult = [NSJSONSerialization JSONObjectWithData:datas options:kNilOptions error:nil];

        data = jsonResult;
        NSMutableArray *names = [NSMutableArray array];
        NSMutableArray *bLinks = [NSMutableArray array];
        NSMutableArray *daThor = [NSMutableArray array];
        NSMutableArray *bsLink = [NSMutableArray array];
        NSMutableArray *ancTxt = [NSMutableArray array];
        NSMutableArray *folLowd = [NSMutableArray array];


        for (id itemfeed in jsonResult){
            [names addObject:[NSString stringWithFormat:@"%@", itemfeed[@"ut"]]];
            [bsLink addObject:[NSString stringWithFormat:@"%@", itemfeed[@"uu"]]];
            [bLinks addObject:[NSString stringWithFormat:@"%@", itemfeed[@"upa"]]];
            [daThor addObject:[NSString stringWithFormat:@"%@", itemfeed[@"pda"]]];
            [ancTxt addObject:[NSString stringWithFormat:@"%@", itemfeed[@"lt"]]];
            [folLowd addObject:[NSString stringWithFormat:@"%@", itemfeed[@"lf"]]];
            self.links = names;
            self.tNames = bLinks;
            self.dThor = daThor;
            self.theLinker = bsLink;
            self.anText = ancTxt;
            self.noDo = folLowd;
        }

        [self.tableView reloadData];


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {


    }];

    [operation start];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    static NSString *Cellidentifier = @"DataTableCellId";
    LICustomCell *cell = (LICustomCell *) [tableView dequeueReusableCellWithIdentifier:Cellidentifier];
    if (cell == nil) {

        cell = [[LICustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier];

        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"LiCellView" owner:self options:nil];
        cell = nib[0];

        NSString *sLink = self.links[indexPath.row];
        NSString *aLink = self.tNames[indexPath.row];
        NSString *aDa = self.dThor[indexPath.row];
        NSString *theInk = self.theLinker[indexPath.row];
        NSString *thAnk = self.anText[indexPath.row];
        NSString *fLink = self.noDo[indexPath.row];

        cell.ageLable.text = theInk;

    }


    return cell;
}

【问题讨论】:

    标签: ios objective-c uitableview nssortdescriptor


    【解决方案1】:

    看起来您的 tableView 的数据位于 6 个不同的数组中。

            NSString *sLink = self.links[indexPath.row];
            NSString *aLink = self.tNames[indexPath.row];
            NSString *aDa = self.dThor[indexPath.row];
            NSString *theInk = self.theLinker[indexPath.row];
            NSString *thAnk = self.anText[indexPath.row];
            NSString *fLink = self.noDo[indexPath.row];
    

    您不应该对所有这些进行排序吗?就您的代码而言,目前尚不清楚 arrayToSort 如何连接到 tableView 的数据模型。你有NSArray *arrayToSort = data;,但不清楚数据在哪里初始化或在哪里设置(似乎你想在你的 JSON 竞争块中设置它)。您还需要调用 [self.tableView reloadData];在 MySegmentControlAction 的末尾。

    您可以创建具有 6 个 NSString 属性的 NSObject 子类,将其称为 MyObject(但更具描述性)。然后执行以下操作:

    for (id itemfeed in jsonResult){
                MyObject *object = [[MyObject alloc]init];
                object.sLink = [NSString stringWithFormat:@"%@", itemfeed[@"ut"]];
                object.aLink = [NSString stringWithFormat:@"%@", itemfeed[@"uu"]];
                ...
    
               [self.data addObject:object];
    
            }
    

    在 JSON 竞赛块中。

    然后您将 cellForRowAtIndexPath 更改为包含类似

    MyObject *object = [self.data objectAtIndex:indexPath.row]
    cell.ageLable.text = object.theInk;
    

    如果你走这条路你也需要更新:

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"pda" ascending:NO];
    

    特别是 @"pda" 到您在 NSObject 子类中命名的任何属性。 @"dThor" 如果您遵循我使用的命名。

    【讨论】:

    • 如何将所有这些数组合二为一?
    • 感谢您的留言。我现在已经按照您的解释进行了设置,但是 sortdescriptor 仍然没有更新。它无法读取数组。
    • 知道了!谢谢你的解释:)
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    相关资源
    最近更新 更多