问题
I have added UIImageView in GMGridView as I want to display the images in grid. After adding imageview in grid all works perfectly. I want to show different image in first row & first column. So I set that image after compairing with index. But when I scroll up & down , it changes the image from first column to 2nd or 3rd column. Also same image is shown in the 6th or 7th row. What going wrong in this? Here is my code
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
GMGridViewCell *cell = [gridView dequeueReusableCell];
UIImageView *imageView = nil;
if (!cell)
{
cell = [[GMGridViewCell alloc] init];
cell.deleteButtonIcon = [UIImage imageNamed:@"close_x.png"];
cell.deleteButtonOffset = CGPointMake(-15, -15);
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
cell.contentView = imageView;
}
NSLog(@"Project Index value:- %d",index);
if (index == 0) {
imageView.image = [UIImage imageNamed:@"add.png"];
}
else{
imageView.image = [UIImage imageNamed:@"face.png"];
}
[[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
return cell;
}
Also do I need [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; ? Can anybody help me ? Thanks
回答1:
Try using the code
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index {
CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
GMGridViewCell *cell = [gridView dequeueReusableCell];
UIImageView *imageView = nil;
if (!cell)
{
cell = [[GMGridViewCell alloc] init];
cell.deleteButtonIcon = [UIImage imageNamed:@"close_x.png"];
cell.deleteButtonOffset = CGPointMake(-15, -15);
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
NSLog(@"Project Index value:- %d",index);
if (index == 0) {
imageView.image = [UIImage imageNamed:@"add.png"];
}
else{
imageView.image = [UIImage imageNamed:@"face.png"];
}
[[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
cell.contentView = imageView;
}
return cell;
}
I think that the problem may be you are removing the cell using makeObjectsPerformSelector
and then it'll never be added again after removing since the control may not enter into the condition if (!cell) {}
due to the presence of the instance cell
.
Hope this helps you.
来源:https://stackoverflow.com/questions/11119758/images-in-imageviews-are-swapping-after-scrolling-gmgridview-up-down-in-iphone