繁体   English   中英

在iOS的UITableView或UICollectionView中重新加载数据的最佳位置是什么

[英]what is the best place to reload data in UITableView or UICollectionView in ios

我正在使用AFNetworking加载数据。就我而言,我是将数据加载到collectionview中。 我将数据加载到自定义方法中,并在其中重新加载collectionview数据。 我在viewDidLoad方法中调用此方法。 我要问的原因是,加载数据需要更多时间。 我认为这是因为我重新加载collectionview数据的地方。我想知道,我是否可以通过以另一种方法(例如viewwillappear或其他任何方法)重新加载collectionview数据来加快此过程。希望有您的帮助。谢谢

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadcategoryData];

    SWRevealViewController *revealcontroller = self.revealViewController;
    if (revealcontroller) {
        [self.sideBarbutton setTarget:self.revealViewController];
        [self.sideBarbutton setAction:@selector(revealToggle:)];
        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    }

    // Do any additional setup after loading the view.
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];


    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

}

- (void)viewDidAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadcategoryData
{
    post = nil;
    NSString *mainurl = [NSString stringWithFormat:@"some url"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    [manager GET:mainurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        posts = (NSDictionary *)responseObject;
        post = [NSMutableArray array];
        for(NSDictionary *all in posts)
        {
            Categories *category = [Categories new];
            category.title = [all objectForKey:@"catname"];
            category.firsturl = [all objectForKey:@"url"];

            [self.maincollectionView reloadData];
            //call for images

            imagespost = nil;
            NSString *imageUrl = [NSString stringWithFormat:@"%@", category.firsturl];
            AFHTTPRequestOperationManager *managerone = [AFHTTPRequestOperationManager manager];
            [managerone GET:imageUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {



                imagesposts = (NSDictionary *)responseObject;
                NSArray *resultone = [imagesposts objectForKey:@"posts"];
                imagespost = [NSMutableArray array];
                if ([resultone count]) {
                    NSDictionary *firstpost = resultone[0];
//                    Categories *newmogocat = [Categories new];


                    NSDictionary *thumbnail_images = [firstpost objectForKeyedSubscript:@"thumbnail_images"];
                    NSDictionary *thumbnail = [thumbnail_images objectForKey:@"thumbnail"];
                    category.imageOneUrl = [NSString stringWithFormat:@"%@",[thumbnail objectForKey:@"url"]];
//                    NSLog(@"%@", newmogocat.imageOneUrl);
//                    [imagespost addObject:newmogocat];

                    [post addObject:category];

                    [self.maincollectionView reloadData];
                }

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//                
//                UIAlertView *erroralert = [[UIAlertView alloc] initWithTitle:@"Something Wrong!" message:[NSString stringWithFormat:@"%@", error.localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
//                [erroralert show];

            }];


        }

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

    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [post count];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellidentifier" forIndexPath:indexPath];
//    cell.layer.borderWidth = 2.0f;
    cell.layer.masksToBounds = NO;
    cell.layer.cornerRadius = 5.0;
    cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
    cell.layer.shadowOffset = CGSizeMake(0, 1);
    cell.layer.shadowRadius = 4.0;
    cell.layer.shadowColor = [UIColor darkGrayColor].CGColor;


    [cell addSubview:cell.maintitleLabel];

    Categories *cat = [post objectAtIndex:indexPath.row];
    [self.maincollectionView reloadInputViews];
    cell.maintitleLabel.text = [NSString stringWithFormat:@" %@ ", cat.title];
    [cell.maintitleLabel sizeToFit];






    NSString *mainstring = [NSString stringWithFormat:@"%@", cat.imageOneUrl];
    NSURL *url = [NSURL URLWithString:mainstring];
//
    [cell.mainImageView setImageWithURL:url placeholderImage:nil];
//



    return cell;
}

最早可以开始加载数据的方法是init方法。 但是老实说,在大多数情况下,他们在彼此追赶后很快就被打了,您甚至都不会注意到其中的区别。 但是,如果您有兴趣,只需在这里进行介绍:

- (id)init
{
    self = [super init];
    if (self) 
    {
        [self loadcategoryData];
    }
    return self;
}

另外我也不知道您要下载多少数据,但是如果加载的列表很长并且花费的时间很长,那么也许您应该考虑进行“分页”(即仅下载数据的一部分,然后请求更多的数据)当用户向下滚动页面时。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM