我有一个UIScrollView,其中包含一系列UIView。每个UIView都包含UIImageView和几个UIButtons。这些UIViews代表'book',它们的数据存储在sqlite数据库中。
BookCover课程:
// 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以反映用户对'book'对象的操作?我已经设置了数据库并且有一个类来处理sql。再问我这样的问题我觉得很蠢。但作为一个客观的初学者,来自专业人士的建议对我来说是巨大的帮助。
任何帮助将不胜感激 谢谢。
答案 0 :(得分:0)
经过一些尝试,我用最简单的方式做到了:
- (void) favouriteButtonTouched:(id)sender
{
UIButton *clickedButton = (UIButton*)sender;
int bookId = clickedButton.tag;
[db openDB];
NSDictionary *bookData = [db getBookById:bookId];
if (bookData != nil)
{
if ([[bookData objectForKey:@"is_fav"] integerValue] == 0) {
[_favouriteButton setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
[db setFavouriteForId:bookId setValue:1];
}
else
{
[_favouriteButton setImage:[UIImage imageNamed:@"star_empty.jpg"] forState:UIControlStateNormal];
[db setFavouriteForId:bookId setValue:0];
}
}
[db closeDB];
}
我仍然不知道改变UIView元素(在这种情况下是UIButton)是否是一个好习惯,每次按下按钮时都会动态调用DB操作。但无论如何这是我现在的解决方案......