如何在背景滚动的同时实现TableView?

时间:2010-03-19 23:01:32

标签: iphone uitableview

想象一下,您有一个普通的桌面视图,其中每一行都是传送带上的一个项目。您将把项目放在表格视图的每个单元格中,但是当您滚动时,您还希望背景图像(传送带)也可以滚动。你怎么能这样做?

4 个答案:

答案 0 :(得分:5)

您应该可以通过设置表格视图的背景颜色来完成此操作:

tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];

答案 1 :(得分:2)

UITableView背景透明,并在其后面添加UIScrollView,其中包含UIImageView。为UITableView滚动时添加一个监听器(因为它是UIScrollView的子类,它具有所有相同的委托方法)。然后,当它滚动时,以编程方式将其后面的UIScrollView的滚动位置设置为相同。

如果你想要反转偏移值,你可以在UIScrollView后面没有第二个UITableView,只需要一个普通的UIIImageView即可。

答案 2 :(得分:1)

我没试过这个,所以我不确定最好的方法是什么,但是一个选择是将你的背景图像作为UIImageView添加到你的每个单元格中,这样每个单元格都有一个全尺寸你的背景图片的副本。在您的单元格上将clipsToBounds设置为NO,并为UIImageView的边界提供一个负y值,该值等于从单元格到表格顶部的偏移量。

您可能还想考虑使用UIScrollView而不是UITableView。

UITableView本身就是一个UIScrollView,因此您可以尝试将您的背景图像添加为UITableView的子视图,但如果有效,我会感到惊讶。我猜UITableView实现不会很好地与外国子视图一起使用。

**编辑**

虽然我仍然怀疑UIScrollView可能是一个更适合在这里使用的基类,但我决定尝试上面描述的UIImageView技巧。只要所有UIImageView共享一个UIImage,它就相当简单并且不会消耗过多的内存。这是我的示例代码:

//  LadderCell.h
#import <UIKit/UIKit.h>

@interface LadderCell : UITableViewCell {
    UIImageView *backgroundImageView;
    UILabel *titleLabel;
}

@property (nonatomic, retain) UIImageView *backgroundImageView;
@property (nonatomic, retain) UILabel *titleLabel;

- (void)setIndexPath:(NSIndexPath *)indexPath;
- (id)initWithImage:(UIImage *)theImage;
+ (NSString *)reuseIdentifier;
+ (CGFloat)height;
@end
//  LadderCell.m
#import "LadderCell.h"

@implementation LadderCell
@synthesize backgroundImageView, titleLabel;

- (void)dealloc {
    self.backgroundImageView = nil;
    self.titleLabel = nil;
    [super dealloc];
}

- (id)initWithImage:(UIImage *)theImage {
    if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[LadderCell reuseIdentifier]]) {
        self.frame = CGRectMake(0.0, 0.0, 320.0, [LadderCell height]);
        self.clipsToBounds = YES;
        self.backgroundImageView = [[[UIImageView alloc] initWithImage:theImage] autorelease];
        backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        self.titleLabel = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];
        titleLabel.textAlignment = UITextAlignmentCenter;
        titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        titleLabel.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
        [self addSubview:backgroundImageView];
        [self addSubview:titleLabel];
    }
    return self;
}

- (void)setIndexPath:(NSIndexPath *)indexPath {
    backgroundImageView.frame = CGRectMake(0.0, 
                                           -(CGFloat)indexPath.row * [LadderCell height] + 100.0,  
                                           backgroundImageView.frame.size.width,
                                           backgroundImageView.frame.size.height);
}

+ (NSString *)reuseIdentifier {
    return @"LadderCell";
}

+ (CGFloat)height {
    return 30;

}

@end
//  TableBackgroundTestViewController.h
#import 

@interface TableBackgroundTestViewController : UITableViewController {
    UIImage *backgroundImage;
}

@property (nonatomic, retain) UIImage *backgroundImage;

@end
//  TableBackgroundTestViewController.m
#import "TableBackgroundTestViewController.h"
#import "LadderCell.h"

@implementation TableBackgroundTestViewController
@synthesize backgroundImage;

- (void)dealloc {
    self.backgroundImage = nil;
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.backgroundImage = [UIImage imageNamed:@"background.png"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return 1000;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [LadderCell height];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LadderCell *cell = (LadderCell *)[tableView dequeueReusableCellWithIdentifier:[LadderCell reuseIdentifier]];
    if (!cell) {
        cell = [[[LadderCell alloc] initWithImage:self.backgroundImage] autorelease];
    }
    [cell setIndexPath:indexPath];
    cell.titleLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];
    return cell;
}

@end

答案 3 :(得分:0)

我的建议类似于Ed Marty(他建议“没有第二个UIScrollView”):

将UITableView放在一个简单的UIView中。使单元格背景透明,以便从tableview下方显示背景

在UITableView下方,放置一个带有所需背景的UIImageView。 UITableView和UIImageView现在都位于同一个封闭的UIView中。

收听UITableView的滚动事件。当检测到滚动时,只需适当地改变背景UIImageView位置(frame.origin.y),这样它就会“坚持”表视图。

你可以将背景作为一个巨大的图像,或者有一系列它们,以便你做平铺。您可以拥有一组图像并在需要时从顶部/底部添加到阵列,还可以删除已从屏幕“滚动”以保存内存的图像。你需要自己计算所有这些背景图像的位置,但没有什么复杂的。