2012-12-02 19 views
0

我有一個包含一系列UIView的UIScrollView。每個UIView包含UIImageView和幾個UIButton。這些UIViews代表'book',它們的數據存儲在sqlite數據庫中。動態地將數據庫值反映到UIView元素

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]; 
} 

我需要改變「最喜歡的按鈕「和」下載按鈕「圖像,並在用戶每次觸摸它們時運行sql,如」UPDATE book SET is_favourite = 1 WHERE book_id = x「。

我應該如何實現這些,以及我應該何時運行sql以反映用戶對「書」對象的操作?我已經建立了數據庫,並有一個類來處理SQL。再次,我覺得很難提出這樣的問題。但作爲一個客觀的初學者,這裏的專業人士的建議對我來說是巨大的幫助。

任何幫助,將不勝感激。
謝謝。

- (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的在這種情況下)一個很好的做法,動態調用數據庫操作:

回答

0

一些嘗試後,我用最簡單的方法做到了每次按下按鈕。但無論如何,這是我現在的解決方案...