代码之家  ›  专栏  ›  技术社区  ›  Jack

uitableview背景图像

  •  4
  • Jack  · 技术社区  · 14 年前

    我试图设置一个背景滚动的表视图。背景是一个重复的云图像。

    作为实现这一点的初步尝试,我使用了以下代码:

    - (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的最佳方式还是资源密集型?

    3 回复  |  直到 14 年前
        1
  •  2
  •   Yarek T Dima    14 年前

    我也遇到过同样的问题,除了在实现的中途,我们决定放弃设计,转而使用自定义的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,没有其他内容 然后在表视图控制器的table view方法中:

    // 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,没有视图,我链接了所有相关的标签,没有更多,它提供了一个很好的图形化方法来编辑标签上的位置,而不需要修改代码,我甚至让我的兄弟修改这个=)

    我希望这有帮助

        2
  •  1
  •   Kristopher Johnson    14 年前
        3
  •  0
  •   Stefan Arentz    14 年前

    你正在设置 UITableView 的背景。不是那个 UITableViewCell 的背景。