您好我正在开发小型IOS应用程序,我是IOS开发的新手。在我的应用程序中,我使用集合视图来显示我的数据。我正在做什么我从我的服务器获取数据,然后在我的UIView控制器中显示。这是我的代码看起来像
- (void)viewDidLoad
{
[super viewDidLoad];
// call for fetching data from server
}
// My collection view methods
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section
{
return [_channelArray count];
}
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.image = // set image from channel array.
return cell;
}
// callback for m web service call
-(void) didReceiveSubscriberChannelList:(SubscriberChannelListDataModel *)subscriberChannelListInfo
{
_channelList = subscriberChannelListInfo;
_channelArray = subscriberChannelListInfo.chanellist;
[collectionView reloadData];
}
以上代码适用于静态数据。但现在我想显示我的服务器数据。我的重装数据不起作用。这该怎么做。需要帮忙。谢谢。
答案 0 :(得分:2)
您是否考虑将回调方法数据放入块中?
// callback for m web service call
-(void) didReceiveSubscriberChannelList:(SubscriberChannelListDataModel *)subscriberChannelListInfo
{
dispatch_async(dispatch_get_main_queue(), ^{
_channelList = subscriberChannelListInfo;
_channelArray = subscriberChannelListInfo.chanellist;
[collectionView reloadData];
});
}
这样,当回调返回时,它将重新加载主线程(UI线程)中的数据