在我的应用程序中,我正在将图像加载到表格的单元格中。但是,当我滚动表格时,图像不在桌面上(向上),即使我向后滚动它也不会在那里。
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
答案 0 :(得分:4)
您的问题最可能是因为您重新使用单元格而不是在它们返回到屏幕时正确地重新定位它们。 好的,所以当一个单元格在一个tableview中离开视图时,它会被卸载以节省内存,当你选择重复使用只有一个标识符的单元格时,这意味着每次使用该标识符的单元格出现在屏幕上时,tableview将返回这种类型的单元格(它不是你第一次配置的原始单元格)当它返回一个圆形时你必须假设它是“脏”的,你必须重新初始化图像及其内容,如果你没有得到像你看到的不受欢迎的结果。
另一个替代方案(虽然对于包含大量单元格的tableviews而言不是很好)是为e ech单元格提供唯一标识符,现在可以保证您的单元格不会“脏”,因为标识符和单元格之间的一对一映射,这样做的缺点是你将耗尽大量的记忆,这会破坏重复使用细胞的目的(就像你根本没有重复使用细胞一样)...... 希望这有帮助
答案 1 :(得分:0)
查看上一篇文章中的代码:
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已经存在了什么