UITableView设置背景图

738 阅读1分钟

修改UITableView的背景图片。

1.图片显示为'PatternImage'模式。

// viewDidLoad

_tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"zrx4.jpg"]];

// cellForRowAtIndexPath

cell.backgroundColor = [UIColor clearColor];

这种情况下背景图片像地板砖一样平铺。拉动tableView背景图片会随着动,若行数超过背景图片的高度,会接着显示下一张图片。

完整代码

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *dataSource;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"查看字体集";
    [self.view addSubview:self.tableView];
}

#pragma mark - lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0.0;
        _tableView.sectionHeaderHeight = 0.0;
        _tableView.sectionFooterHeight = 0.0;
        _tableView.estimatedSectionHeaderHeight = 0.0;
        _tableView.estimatedSectionFooterHeight = 0.0;
        _tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"zrx4.jpg"]];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableView class])];
    }
    return _tableView;
}

#pragma mark - lazyLoding
- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [UIFont familyNames];
    }
    return _dataSource;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableView class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.dataSource[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

@end

2.正常的背景图片。

// viewDidLoad

_tableView.backgroundColor = [UIColor clearColor];

_tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zrx4.jpg"]];

// cellForRowAtIndexPath

cell.backgroundColor = [UIColor clearColor];

这种情况下背景图片不会动,即无论多少行看到的都是同样的背景。

完整代码

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *dataSource;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"查看字体集";
    [self.view addSubview:self.tableView];
}

#pragma mark - lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0.0;
        _tableView.sectionHeaderHeight = 0.0;
        _tableView.sectionFooterHeight = 0.0;
        _tableView.estimatedSectionHeaderHeight = 0.0;
        _tableView.estimatedSectionFooterHeight = 0.0;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zrx4.jpg"]];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableView class])];
    }
    return _tableView;
}

#pragma mark - lazyLoding
- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [UIFont familyNames];
    }
    return _dataSource;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableView class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.dataSource[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

@end