0

我有一个包含一系列 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初学者,这里专业人士的建议对我帮助很大。

任何帮助,将不胜感激。
谢谢你。

4

1 回答 1

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 操作。但无论如何,这是我现在的解决方案......

于 2012-12-20T04:41:37.717 回答