【问题标题】:Image on uitableview lost after scrolling滚动后 uitableview 上的图像丢失
【发布时间】:2009-12-02 08:51:23
【问题描述】:

在我的应用程序中,我正在将图像加载到表格的单元格中。但是,当我滚动表格时,图像会消失在表格的视线范围内(向上),即使我向后滚动也不会有。

customCell.h

#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell<UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
UIViewController *viewController;
UIImageView *imageView;
UIButton *button;
@property (nonatomic, retain) IBOutlet UIImageView *imageView; 
@property (nonatomic, assign)UIViewController *viewController;
@property (nonatomic, retain) IBOutlet UIButton *button;
-(IBAction)selectExistingPicture1;

@end
}

customCell.m

    #import "CustomCell.h"
    @implementation CustomCell
@synthesize imageView;
@synthesize viewController;
@synthesize button;
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 
    [picker dismissModalViewControllerAnimated:YES]; 
} 

- (IBAction)selectExistingPicture1 { 
        if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self; 
        picker.allowsImageEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self.viewController presentModalViewController:picker animated:YES];
        [picker release];
    } 
    else { 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error accessing photo library" message:@"Device does not support a photo library" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil]; 
        [alert show]; 
        [alert release]; 
    } 
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {


    UIImage *thumbnail = [image _imageScaledToSize:CGSizeMake(80, 80) interpolationQuality:1];
    /*CGSize newSize = CGSizeMake(80, 80);
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();*/
    imageView.image = thumbnail;

    [picker dismissModalViewControllerAnimated:YES]; 


}  

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // Initialization code
    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)dealloc {
    [imageView release];
    [viewController release];
    [button release];

    [super dealloc];
}


@end

cameraViewController.h

#import <UIKit/UIKit.h>

@interface Camera1ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {      

}

@end

cameraViewController.m

#import "Camera1ViewController.h"
#import "CustomCell.h"

@implementation Camera1ViewController

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [i
    [super dealloc];
}


 #pragma mark Table Data Source Methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 25; 
} 

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

          CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
     if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CustomCellIdentifier] autorelease];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self
                                                    options:nil];
         for (id currentObject in nib){
             if ([currentObject isKindOfClass:[CustomCell class]]){
                 cell = (CustomCell *)currentObject;
                 cell.viewController = self;
                 break;
             }
         }

     }
     //dont know what to do here. 
     return cell;

} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 100; 
} 


- (NSString *)tableView:(UITableView *)tableView 
titleForHeaderInSection:(NSInteger)section { 
    NSString *key = @"Album"; 
    return key; 
} 
@end

【问题讨论】:

    标签: iphone uitableview uiimage reload


    【解决方案1】:

    您的问题很可能是由于您重复使用了单元格,并且当它们重新出现在屏幕上时没有正确地重新初始化它们。 好的,所以当一个单元格在表格视图中超出视图时,它会被卸载以节省内存,当您选择重用一个只有 1 个标识符的单元格时,这意味着每次使用该标识符的单元格出现在屏幕上时,表格视图都会返回该类型的单元格(但它不是您第一次配置的原始单元格)当它返回一轮时,您必须假设它是“脏的”并且您必须重新初始化图像及其内容,如果你不你会得到像您看到的那样不受欢迎的结果。

    另一种选择(尽管对于具有大量单元格的表格视图来说不是一个好的选择)是给每个单元格一个唯一的标识符,现在您可以保证您的单元格不会“脏”,因为在标识符和单元格之间存在一对一的映射,这样做的缺点是您将使用大量内存,这违背了重用单元的目的(就好像您根本没有重用单元)...... 希望这会有所帮助

    【讨论】:

    • 在这里阅读 UITableView 编程指南,这可能会有所帮助,developer.apple.com/iphone/library/documentation/UserExperience/…
    • 如果你发布你的 cellForRowAtIndexPath 代码我可能会帮助你
    • 我已经添加了我的代码。还有一件事要对大家说,我是编程方面的新手,现在也是一家公司的实习开发人员。他们要求我自己了解 iphone SDK 和开发。这里没有人知道。我真的很痛苦。请帮忙。这是第一次安排
    【解决方案2】:

    通过查看您在上一篇文章中的代码:

    NSUInteger s= indexPath.section;
     //[cell setText:[NSString stringWithFormat:@"I am cell %d", indexPath.row]];
    
     NSUInteger r = indexPath.row;
      cell.imageView.image = nil;
     for (s;s==0;s++)
     for(r;r==0;r++)
     {           
          UIImage *img=imageView.image;
         cell.imageView.image = img;
         }
     return cell;
    

    }

    我不确定循环的目的是什么,但是任何时候需要重绘单元格时,您都需要将单元格的 UIImageView 设置为 imageView.image(我假设 imageView 是您的 UIViewController 的一个属性)所以本质上您正在覆盖什么是已经有了那个单一的 imageView

    【讨论】:

    • 您好,感谢您的回复。我对自己代码的 cellForRowAtIndexPath 部分感到困惑。我不知道该怎么处理那部分。我正在使用自定义单元格,在其中创建一个属性 imageView(customCell.h)。该单元格中有一个 imageView 和一个 uibutton。我现在将代码粘贴到问题中。我正在从 customCell.m 中定义一个操作方法来修饰按钮(selectExistingPicture)。
    猜你喜欢
    • 2015-08-31
    • 1970-01-01
    • 2011-01-13
    • 2013-04-26
    • 1970-01-01
    • 2021-12-11
    • 2016-07-07
    • 1970-01-01
    • 2021-07-19
    相关资源
    最近更新 更多