我一直在创建一个收集mysql数据的been服务,这些数据将使用AFNetworking发送到NSMutableArray imagesArray中的应用程序
我使用DTCoreText提取图像urls,然后将它们放入collectionView单元格中。
问题是,当我导航到另一个ViewController并返回时,发生了一件奇怪的事情。不包含图像的cell获取另一个单元中的图像。
图像插图:
正如你所看到的,我已经做了一个if和else的声明,所以一个图像不应该有一个图像和相机图像?
应用程序将打开:

在我导航到另一个viewController并返回到这个之后:

我的代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
collectionCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
UIColor * borderColor = [UIColor colorWithRed:226/255.0f green:221/255.0f blue:219/255.0f alpha:1.0f];
cell.backgroundColor = [UIColor lightGrayColor];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
cell.self.theView = [[UIView alloc] initWithFrame:CGRectMake(0, 200-50, screenWidth/2-8, 50)];
cell.self.theView .backgroundColor = [UIColor whiteColor];
[cell.contentView addSubview:cell.self.theView];
cell.self.theTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, screenWidth/2-8-5, 30)];
cell.self.theTitle.textColor = tintColor;
[cell.self.theTitle setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:12]];
cell.self.theTitle.text = [[imagesArray valueForKey:@"title"] objectAtIndex:indexPath.row];
[cell.self.theView addSubview: cell.self.theTitle];
UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.self.theView.frame.size.height-2, cell.self.theView.frame.size.width, 2)];
bottomLineView.backgroundColor = borderColor;
[cell.self.theView addSubview:bottomLineView];
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray* matches = [detector matchesInString:[[imagesArray valueForKey:@"content"] objectAtIndex:indexPath.row] options:0 range:NSMakeRange(0, [[[imagesArray valueForKey:@"content"] objectAtIndex:indexPath.row] length])];
NSArray *imageExtensions = @[@"png", @"jpg", @"gif", @"jpeg"];
Images = [[NSMutableArray alloc] init];
// Iterate & match the URL objects from your checking results
for (NSTextCheckingResult *result in matches) {
NSURL *url = [result URL];
NSString *extension = [url pathExtension];
if ([imageExtensions containsObject:extension]) {
NSLog(@"Image URL: %@", url);
[Images addObject:url];
} else {
}
}
if(Images.count > 0)
{
cell.self.theImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth/2-8, 150)];
UIImage* myImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@",[Images objectAtIndex:0] ]]]];
[cell.self.theImage setImage:myImage];
[cell.contentView addSubview:cell.self.theImage];
} else {
cell.self.theImage = [[UIImageView alloc] initWithFrame:CGRectMake((screenWidth/2-8-5)/2-25, 80-25, 50, 50)];
UIImage* myImage = [UIImage imageNamed:@"camera"];
[cell.self.theImage setImage:myImage];
[cell.contentView addSubview:cell.self.theImage];
}
return cell;
}ViewWillAppear
-(void)viewWillAppear:(BOOL)animated {
imagesArray = [[NSMutableArray alloc] init];
[imagesArray removeAllObjects];
[self getImages];
} 发布于 2014-04-20 19:33:51
首先,阅读一些关于单元重用机制的教程。当您第一次调用dequeueReusableCellWithReuseIdentifier时,您将获得新分配的单元格(假设这些单元格来自故事板,或者您注册了单元格类并将其分配给标识符"cell")。
当它是一个新创建的单元格对象时,您可以创建所有的子视图,并将它们添加为子视图。但是,当用户进一步向下或向上滚动时,dequeueReusableCellWithReuseIdentifier将返回一个已经拥有其子视图的现有单元格。在这种情况下,您只需要刷新它们的内容。但是您可以创建新的标签和图像视图等,并将它们放在相同的位置,这样它们就可以隐藏(在最好的情况下)以前存在的原始UI元素。在最好的情况下,这只是某种内存泄漏。
坦率地说,我不知道在用户使用选项卡栏导航回到视图后,是否会为每个可见单元格再次调用cellForItemAtIndexPath。我不这么认为,但也不会感到惊讶。
直到你真正理解了这种机制并相应地设计了你的代码,奇怪的效果才会出现。这就是为什么我建议首先修复这些问题,并设计您的cellForItemAtIndexPath,使其符合重用机制。这将解决大多数像你这样的问题。如果它不能解决它,那么给我们一个新的问题和重构的可口可乐。
https://stackoverflow.com/questions/23181156
复制相似问题