我有一个工作UICollectionViewCell
在UITableViewCell
。我正在使用HWViewPager,因此collectionview
单元格从左向右移动。不幸的是,直到用户向左或向右滚动才更新数据。填充单元格的内容是从中获取的json
,因此我认为这可以做一些事情,但我不确定该做什么。我将所有内容加载到awakeFromNib中。
- (void)awakeFromNib {
] videoArray = [[NSMutableArray alloc] init];
[self getDisco];
}
getDisco是void函数。
- (void)getDisco
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
videoArray = [NSMutableArray arrayWithArray:[responseObject valueForKey:@"releases"]];
// NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
现在,问题出在哪里。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.item < [videoArray count]){
DiscoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell2" forIndexPath:indexPath];
NSDictionary *shot = [videoArray objectAtIndex:[indexPath row]];
cell.label2.text = [shot objectForKey:@"title"];
return cell;
return nil;
}else{
DiscoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell2" forIndexPath:indexPath];
cell.label2.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];
return cell;
}
}
如果没有if / else语句,viewcontroller
崩溃将完全崩溃。有了它,在用户滚动之前,没有任何更新。我的问题是如何为用户预装所有内容而不会崩溃?请保持UICollectionViewCell
在tableviewcell
。
您应该在完成请求后重新加载collectionView:
- (void)getDisco
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://musicbrainz.org/ws/2/release/?query=arid:e0140a67-e4d1-4f13-8a01-364355bee46e%20AND%20primarytype:single&fmt=json&limit=100" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
videoArray = [NSMutableArray arrayWithArray:[responseObject valueForKey:@"releases"]];
// NSLog(@"JSON: %@", responseObject);
[collectionView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句