滚动后uitableview上的图像丢失 [英] Image on uitableview lost after scrolling

查看:45
本文介绍了滚动后uitableview上的图像丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在将图像加载到表的单元格中.但是,当我滚动表格时,图像看不到表格(向上),即使我向后滚动也不会在那里.

in my application, i'm loading images to the cells of a table. But, when i scroll the table, the image goes out of sight of the table(goes up), it wont be there even if i scroll back.

customCell.h

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

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

cameraViewController.h

#import <UIKit/UIKit.h>

@interface Camera1ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {      

}

@end

cameraViewController.m

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

推荐答案

您的问题很可能是由于您重新使用单元格,并且当它们再次出现在屏幕上时,它们没有正确地重新初始化.好的,因此当表格视图中某个单元格消失时,将其卸载以节省内存,当您选择重复使用只有一个标识符的单元格时,这意味着每次使用该标识符的单元格出现在屏幕上时,表格视图都会返回这种类型的单元格(不要将其不是您初次配置的原始单元格)在回合时必须假定它是脏的",并且必须重新初始化图像及其内容,否则您将得到像您看到的那样不期望的结果.

Your problem is mostlikely due to you reusing cells and not reintializing them correctly when they come back on the screen. OK, so when a cell goes out of view in a tableview, it is unloaded to save memory, when you choose to reuse a cell with only 1 identifier it means that everytime a cell that used that identifier comes on screen the tableview will give back a cell of that type (BUt its NOT the original cell that you configured the first time around) when it comes back a round you must assume that it is "dirty" and you must reinitialize the image and its content, if you dont youll get undesired results like you are seeing.

另一种替代方法(尽管对于具有许多单元格的表视图来说不是一个好方法)是为每个单元格提供唯一的标识符,现在可以确保您的单元格不会脏",因为在标识符和单元格之间存在一对一的映射,这样做的缺点是您将耗尽大量内存,从而无法实现重用单元的目的(就好像您根本没有重用单元一样)...希望这会有所帮助

Another alternative (though not a good one for tableviews with lots of cells) is to give e ach cell a unique identifier, now you are guaranteed your cells wont be "dirty" because theres a one to one mapping between identifier and cells, the down side of this is that you will be using up lots of memory, which defeats the purpose of reusing cells (its as if y ou werent reusing cells at all)... Hope this helps

这篇关于滚动后uitableview上的图像丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆