簡體   English   中英

UITableView背景圖片

[英]UITableView Background Image

我正在嘗試設置一個滾動背景的表格視圖。 背景是重復的雲圖像。

作為實現這一點的初步嘗試,我使用了以下代碼:

- (void)viewDidLoad {
    aTableView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"cloud.jpg"]];
    aTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

但是,這並不像我希望的那樣有效。 圖像作為背景放置到每個單元格(這很好),但也作為表格視圖的背景,因此當用戶滾動超過屏幕邊界並使用反彈時,將顯示相同的雲背景。 這會產生不希望的重疊效果。

我想要的是每個單元格都有這個重復的圖像(以便它與表格一起滾動),但是對於表格視圖的背景是純黑色。 這可能嗎?

我知道我可以將圖像添加到自定義單元格並將其發送到后面,但這看起來像是太多的內存占用解決方法。

干杯


我已設法使用以下代碼對此進行排序:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        [cell setBackgroundColor:[UIColor colorWithPatternImage: [UIImage imageNamed: @"cloud.jpg"]]];
}

這是將UIColor設置為UIImage的最佳方式還是這種資源密集型?

我有完全相同的問題,除了實現的一半,我們決定廢棄設計並轉而使用自定義tableview單元格。 我們的原始設計看起來像一個記事本,每行都有單元格。

對UITableViewCell進行子類化可能看起來是一項艱巨的任務,但如果您將來需要自定義單元格並且看起來它會按照您的要求進行操作,那么它是值得的。 如果您感興趣,我可以從我的項目中為您發布代碼。但是,請注意,您不會在UITableView本身上具有背景,僅在單元格上。

編輯:發布與創建自定義UITableViewCell相關的代碼:我的單元名為HotOffersCell,這是HotOffersCell.h

#import <UIKit/UIKit.h>
@interface HotOffersCell : UITableViewCell {
    IBOutlet UILabel *mainLabel;
    IBOutlet UILabel *subLabel;
    IBOutlet UILabel *price;
    IBOutlet UIImageView *imageView;
}
@property (nonatomic, retain) UILabel *mainLabel;
@property (nonatomic, retain) UILabel *subLabel;
@property (nonatomic, retain) UILabel *price;
@property (nonatomic, retain) UIImageView *imageView;

@end

HotOffersCell.m非常簡單,只是sysnthesize所有屬性,沒有別的然后在表視圖控制器的TableView方法中:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"HotOffersCell";

    HotOffersCell *cell = (HotOffersCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HotOffersCell" owner:nil options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (HotOffersCell *) currentObject;
                break;
            }
        }
    }
    NewsItem *newsItem = [newsList objectAtIndex:indexPath.row];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"d MMM YY"];
    NSString *date = [formatter stringFromDate:newsItem.departure_date];
    if (indexPath.row % 2 == 0) {
        cell.contentView.backgroundColor = [UIColor colorWithWhite:230.0/255 alpha:1]; // Match Colour
    } else {
        cell.contentView.backgroundColor = [UIColor colorWithWhite:242.0/255 alpha:1]; // Match Colour
    }

    cell.mainLabel.text = newsItem.destination;
    cell.subLabel.text = [[NSString alloc] initWithFormat:@"%@ - %@ nights", date, newsItem.nights];
    if ([self.navigationController.title isEqualToString:@"Top 20"]) {
        cell.price.text = newsItem.price_inside;
    } else {
        cell.price.text = newsItem.price_balcony;
    }
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", newsItem.cruise_line]];

    [formatter release];        
    return cell;
}

我還有一個HotOffersCell.xib,這個文件包含一個UITableViewCell,沒有視圖,我已經鏈接了所有相關標簽,僅此而已,它提供了一種很好的圖形方式來編輯標簽上的展示位置,而無需更改代碼,我甚至讓我的兄弟修改這個=)

我希望這有幫助

這可能有助於設置UITableView的背景顏色: 在分組樣式的UITableView中更改背景顏色和截面標題文本顏色

您正在設置UITableView的背景。 不是UITableViewCell的背景。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM