0
我正在使用AFNetworking加載數據。在我的情況下,我加載數據到一個collectionview。 我加載我的自定義方法中的數據,並在裏面我重新加載collectionview數據。 而我在viewDidLoad方法中調用這個方法。爲什麼我問這是,它需要更多的時間來加載數據。我想這是因爲地方的我重裝的CollectionView data.I想知道,我可以像viewWillAppear中的另一種方法重載的CollectionView數據加快這一進程,或任何other.hope你你help.thank什麼是最好的地方重新加載UITableView或UICollectionView中的數據ios
- (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;
}
我該怎麼做分頁,你能告訴我該怎麼做。到ios @ villy393 –
那麼它更依賴於提供結果的服務器而不是你,例如你加載頁面然後請求20個結果,當用戶滾動某個點時,你需要下一個20結果,所以你需要想象用一些方法告訴服務器你想要更多的結果以及你想要的結果。檢查這個傢伙無限滾動項目的代碼:https://github.com/JigarM/Infinite-UIITableview-Scroll(注意它全部在Swift中) – villy393
或者查看這個http://jslim.net/blog/2014/04/01/infinite-scroll-using-on-uitableview/ – villy393