這是我做過什麼:
一個。重寫GalleryButton
類的- initWithFrame:
方法是這樣的:
- (id)initWithFrame:(CGRect)frame imageName:(NSString *)imgName
{
self = [super initWithFrame:frame];
if (self) {
isInScrollview = YES;
CGRect myImageRect = CGRectMake(0, 0, 64, 64);
images = [[UIImageView alloc] initWithFrame:myImageRect];
// here's the change:
// instead of a constant name, use the `imgName` parameter
[images setImage:[UIImage imageNamed:imgName]];
[self addSubview:images];
self.backgroundColor = [UIColor blackColor];
self.layer.borderWidth = 2;
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 5;
}
return self;
}
然後,改寫GalleryScrollView
類的這樣的- addAttachment:
方法:
- (void) addAttachment:(AttachmentItem *)attachment withImageNamed:(NSString *)imgName
{
// everything stays the same (!), except this line:
GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height)];
// is to be extended to:
GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height) imageName:imgName];
...
}
然後,在- [HegakaDragAndDropRecycleBinViewController viewDidLoad]
,指定圖像的文件名,你要使用:
[self.gallery addAttachment:item withImageNamed:@"recyclebin"];
[self.gallery addAttachment:item withImageNamed:@"canadian-maple"];
[self.gallery addAttachment:item withImageNamed:@"light-cherry"];
[self.gallery addAttachment:item withImageNamed:@"mozambique-wenge"];
[self.gallery addAttachment:item withImageNamed:@"canadian-maple"];
結果:
我爲什麼會得到一個錯誤在這條線:GalleryButton * btnAttachment = [[GalleryButton的alloc] INIT:CGRectMake(STARTX,startY,寬度,高度)imageName:imgName];在我的GalleryScrollView.m「實例方法-init:imageNamed找不到? –
我將如何聲明這我我的頭文件? –
它現在的作品,但我在HegakaDragAndDropRecycleBinViewController viewdidload添加附件withImageNamed未找到與我的圖像[ self.gallery addAttachment:item withImageNamed:@「recyclebin」]; –