nup*_*pac 2 objective-c ios uicollectionview
所以我想要实现的是当用户从UICollectionView点击一个单元格时,来自该单元格的图像显示在下一个UIView上.
为了实现这一点,我使用了委托方法-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath和NSUserDefaults.这就是我这样做的方式
这是代码:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NewsfeedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSData *data = [[NSData alloc]initWithData:UIImagePNGRepresentation(cell.ItemImageView.image)];
NSLog(@"Before Data %@", data);
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:data forKey:@"feedData"];
[def synchronize];
[self performSegueWithIdentifier:@"itemTappedSegue" sender:self];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSData *data = [def objectForKey:@"feedData"];
NSLog(@"After Data:%@", data);
UIImage *image = [[UIImage alloc]initWithData:data];
self.imageView.image = image;
}
Run Code Online (Sandbox Code Playgroud)
应该工作,但不是.我得到随机结果.有时在下一个UIView中没有图像,有时会有图像,但它不是我拍摄的图像.
编辑::这里是实现 cellForItemAtIndexpath
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
NewsfeedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
if(response2.count > 0){
cell.usernameLabel.text = [self getUsername:[response2 objectAtIndex:indexPath.item]];
[UIApplication sharedApplication].networkActivityIndicatorVisible =YES;
dispatch_queue_t getUserAvatar = dispatch_queue_create("Avatar downloader", NULL);
dispatch_queue_t getFeed = dispatch_queue_create("Feed downloader", NULL);
dispatch_async(getUserAvatar, ^{
NSString *urlString = [self getAvatarUrl:[response2 objectAtIndex:indexPath.item]];
NSURL *url = [[NSURL alloc]initWithString:urlString];
NSData *avatarData = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
cell.DPImageView.layer.masksToBounds = YES;
cell.DPImageView.layer.cornerRadius = 6.0f;
cell.DPImageView.image = [UIImage imageWithData:avatarData];
});
});
dispatch_async(getFeed, ^{
NSString *URLString = [self getFeedUrl:[response2 objectAtIndex:indexPath.item]];
NSURL *URL = [[NSURL alloc]initWithString:URLString];
NSData *feedData = [NSData dataWithContentsOfURL:URL];
dispatch_async(dispatch_get_main_queue(), ^{
cell.ItemImageView.image = [UIImage imageWithData:feedData];
cell.ItemImageView.layer.masksToBounds = YES;
cell.LikeBtn.hidden = NO;
cell.CommentBtn.hidden = NO;
cell.usernameLabel.hidden = NO;
cell.DPImageView.hidden = NO;
});
});
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
else{
//cell.ItemImageView.image = [UIImage imageNamed:@"NoFeed.png"];
cell.LikeBtn.hidden = YES;
cell.CommentBtn.hidden = YES;
cell.usernameLabel.hidden = YES;
cell.DPImageView.hidden = YES;
}
return cell;
Run Code Online (Sandbox Code Playgroud)
Avi*_*dok 13
你为什么这样做:
NewsfeedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
?您需要获取indexPath的单元格,而不是从池中创建单元格.使用 :
NewsFeedCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
代替.
| 归档时间: |
|
| 查看次数: |
10262 次 |
| 最近记录: |