我有一个包含一系列 UIView 的 UIScrollView。每个 UIView 包含 UIImageView 和几个 UIButtons。这些 UIView 代表“书”,它们的数据存储在 sqlite 数据库中。
封面类:
// Cover
_coverImageView = [[UIImageView alloc] initWithFrame:CGRectMake(SHELF_CELL_PADDING_X, SHELF_CELL_PADDING_Y, SHELF_CELL_COVER_WIDTH, SHELF_CELL_COVER_HEIGHT)];
[self addSubview:_coverImageView];
// Label
_coverTitle = [[UILabel alloc] initWithFrame:CGRectMake(SHELF_CELL_PADDING_X, SHELF_CELL_COVER_HEIGHT, SHELF_CELL_TITLE_WIDTH, SHELF_CELL_TITLE_HEIGHT)];
_coverTitle.backgroundColor = [UIColor clearColor];
_coverTitle.textAlignment = UITextAlignmentCenter;
_coverTitle.lineBreakMode = UILineBreakModeWordWrap;
_coverTitle.numberOfLines = 0;
//_coverTitle.font = [_coverTitle.font fontWithSize:10];
_coverTitle.font = [UIFont fontWithName:@"Arial-BoldMT" size:10];
[self addSubview:_coverTitle];
// New Label
_labelNew = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 6)];
[self addSubview:_labelNew];
// Favourite Button
_favouriteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_favouriteButton.frame = CGRectMake(SHELF_CELL_COVER_WIDTH - 5, -5, 23, 23);
[_favouriteButton addTarget:self action:@selector(favouriteButtonTouched) forControlEvents:UIControlEventTouchDown];
[self addSubview:_favouriteButton];
// Download Button
_downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_downloadButton.frame = CGRectMake(SHELF_CELL_COVER_WIDTH - 5, SHELF_CELL_COVER_HEIGHT - 20, 23, 23);
[_downloadButton addTarget:self action:@selector(downloadButtonTouched) forControlEvents:UIControlEventTouchDown];
[self addSubview:_downloadButton];
在“图书列表视图”中,我只是为每个 UIView(BookCover) 设置了实际数据,如下所示:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SHELF_SEARCH_HEIGHT + SHELF_SECTION_HEIGHT + SHELF_HEIGHT + SHELF_SECTION_HEIGHT + 5, self.view.frame.size.width, SHELF_HEIGHT)];
NSUInteger BookCoverCount = [pamphletList count];
for (int i=0; i<BookCoverCount; i++) {
CGFloat coverX = i * 100;
BookCover *bookCover = [[BookCoverView alloc] initWithFrame:CGRectMake(coverX, 10, SHELF_CELL_WIDTH, SHELF_CELL_HEIGHT)];
bookCover.backgroundColor = [UIColor clearColor];
// Set an image for cover...
NSDictionary *bookData = [bookList objectAtIndex:i];
NSString *bookId = [bookData objectForKey:@"id"];
NSString *bookImageName = [bookData objectForKey:@"image"];
NSString *bookImageDir = [NSString stringWithFormat:@"%@/%@/%@", documentDir, BOOK_FOLDER, bookId];
UIImage *bookImageFile = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", bookImageDir, bookImageName]];
bookCover.coverImageView.image = bookImageFile;
bookCover.coverTitle.text = [bookData objectForKey:@"title"];
bookCover.userInteractionEnabled = YES;
bookCover.tag = i;
[scrollView addSubview:bookCover];
}
每次用户触摸它们时,我都需要更改“收藏按钮”和“下载按钮”图像并运行类似“UPDATE book SET is_favourite = 1 WHERE book_id = x”的 sql。
我应该如何实现这些,何时应该运行 sql 以将用户的操作反映到“书”对象?我已经建立了数据库并有一个类来处理 sql。再一次,我觉得问这样的问题太愚蠢了。但是作为一个objective-c初学者,这里专业人士的建议对我帮助很大。
任何帮助,将不胜感激。
谢谢你。