首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSSortDescriptor不更新UITableView

NSSortDescriptor不更新UITableView
EN

Stack Overflow用户
提问于 2015-11-08 21:20:36
回答 1查看 79关注 0票数 0

我正在尝试使用NSSortDescriptor在UISegmentedControl中对我的表视图进行排序。当我记录数组排序时,它会显示正确的排序顺序,但是表视图在调用self.tableView reloadData之后不会更新;

数据来自一个由json提要填充的数组。我不使用NSObjects来显示表视图,它都是从NSArray填充的。见下面的代码:

代码语言:javascript
复制
@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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-08 21:50:55

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

代码语言:javascript
复制
        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个NSObject属性的NSString子类,将其称为MyObject (但更具有描述性)。然后做一些如下的事情:

代码语言:javascript
复制
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更改为包含以下内容

代码语言:javascript
复制
MyObject *object = [self.data objectAtIndex:indexPath.row]
cell.ageLable.text = object.theInk;

如果您走这条路线,您还需要更新:

代码语言:javascript
复制
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"pda" ascending:NO];

具体而言,@"pda“无论您在NSObject子类中将属性命名为什么。@"dThor“,如果您按照我使用的命名。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33599164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档