关于Objective C:如何更改iOS” UITableView负空间”颜色?

How to change iOS “UITableView negative space” color?

我将" UITableView负数空间"定义为从最后一个单元格底部到表底部的空间。我只想更改该空间的颜色,而不是tableView的backgroundColor属性。

是的,我可以设置tableView的背景颜色以强制该空间为特定颜色,这是事实,但是我也希望tableView的背景颜色清晰。

将tableView的背景设置为清除的原因是,当发生滚动跳动或进行拉动刷新时,用户可以在tableView后面看到。

使用页脚可能是可行的,但是我不确定如何以动态的方式使用各种页眉和不同大小的单元格正确调整页脚的大小。

问这个问题的另一种方法:如何确定负空间的大小,以便仅用任何颜色创建该大小的页脚视图?


您可以从UITableViewcontentSize及其bounds的末尾计算面积,如果它的contentSize小于界限,请在tableView

上方添加一个视图

我做了一个快速测试,看它是否会或多或少地起作用

所有魔力发生在addNegativeSpaceView中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@interface TableViewController ()
@property (strong, nonatomic) UIView *negativeSpaceView;
@end

@implementation TableViewController

#pragma mark - View Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];

    // Remove all the extra lines below last item
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self addNegativeSpaceView];
}

#pragma mark - Implementation
- (void)addNegativeSpaceView {
    if (self.tableView.contentSize.height < self.tableView.bounds.size.height) {
        CGSize size = CGSizeMake(self.tableView.bounds.size.width, self.tableView.bounds.size.height - self.tableView.contentSize.height);
        CGPoint origin = CGPointMake(0, self.tableView.contentSize.height);
        self.negativeSpaceView.frame = (CGRect){origin, size};
        [self.view insertSubview:self.negativeSpaceView aboveSubview:self.tableView];
    } else {
        [self.negativeSpaceView removeFromSuperview];
    }
}
#pragma mark - Getter
- (UIView *)negativeSpaceView {
    if (!_negativeSpaceView) {
        _negativeSpaceView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
        _negativeSpaceView.backgroundColor = [UIColor lightGrayColor];
    }
    return _negativeSpaceView;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    // Configure the cell...
    cell.contentView.backgroundColor = [UIColor randomColor];
    return cell;
}

结果为kind