知識點總結(jié)24:tableview設(shè)置footerview注意點

1.tableview設(shè)置footerview之前一定要先設(shè)置高度,不然tableview的contentsize是沒有將footerview的高度計算入內(nèi)的.

// 控制器.m文件,該控制器有個tableview要設(shè)置footer view
@implementation ZGKMeViewController

// 重寫init方法,將Group樣式封裝起來
- (instancetype)init{
    // 因為是封裝起來,所以不是調(diào)用super的方法
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    footerView.backgroundColor = [UIColor greenColor];
    // 設(shè)置footerview前要設(shè)置其高度,或者封裝在footerview內(nèi)部完成高度的設(shè)置,否則footer是正常無法滾動
    self.tableView.tableFooterView = footerView;
    footerView.tableview = self.tableView;
}

  • footerview內(nèi)部完成高度的設(shè)置,還要tableview的reloadData方法配合,單單改變tableview的contentsize是有bug的
// 在footerview的.m文件

// 6.根據(jù)返回的數(shù)據(jù),重新設(shè)置footerview的高度,高度=最后一個按鈕的bottom的最大y值
        // 6.1設(shè)置了footerview的高度,但是tabelview的contentsize沒有設(shè)置,所以無法滾動
        self.zgk_height = self.subviews.lastObject.zgk_bottom;
        // 6.2設(shè)置重新設(shè)置tableView的contentsize,但是跳轉(zhuǎn)其他界面再返回的時候,就不管用了
        NSLog(@"self.tableView = %@", self.tableview);
        // 方法一:通過屬性傳入(bug:僅僅修改contentsize,再次返回我的控制器時,就不能上下滾動了)
//        self.tableview.contentSize = CGSizeMake(0, self.zgk_bottom);
        

        // 方法二:通過小面包觀察,知道footerview的superview就是tableview(bug:僅僅修改contentsize,再次返回我的控制器時,就不能上下滾動了)
//        UITableView *tableView = (UITableView *)self.superview;
//        tableView.contentSize = CGSizeMake(0, self.zgk_bottom);
        
        // 方法三:使用reloadData,讓tableview重新計算tableview的contentsize
        [self.tableview reloadData];

// 注意:如果這里重新設(shè)置tableview的footerview,則tableview的content size會比實際的contentsize多20的高度,這可能是蘋果的bug
// self.tableview.footerview= self;
  • 強制改變tableview的contentsize會有bug,雖然第一次footer view正常滾動,但是再次跳轉(zhuǎn)該控制器的時候footerview仍然無法正常滾動,所以請求數(shù)據(jù)完畢后,要調(diào)用tableview的reloadData方法,重寫計算tableview的高度和contentsize.相類似的還有如下:
        [self.tableview reloadData];
        [self.tableview setNeedsLayout];
        [self.tableview setNeedsDisplay];
        [self.tableview layoutIfNeeded];

2.footerview自定義button需要注意的2點

#import "ZGKMeFooterviewButton.h"

@implementation ZGKMeFooterviewButton
// 一次性初始化設(shè)置,寫在initWithFrame,xib寫在awakeFromNib,tableViewCell寫在initWithStyle
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        
        // 設(shè)置按鈕標(biāo)題顏色
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
    }
    
    return self;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    // 注意1:通過用比例來設(shè)定,可以使得不同尺寸的屏幕可以自適應(yīng)
    self.imageView.zgk_y = self.zgk_height * 0.1;
    self.imageView.zgk_height = self.zgk_height * 0.5;
    self.imageView.zgk_width = self.imageView.zgk_height;
    self.imageView.zgk_centerX = self.zgk_width * 0.5;
    
    self.titleLabel.zgk_y = self.imageView.zgk_bottom;
    self.titleLabel.zgk_height = self.zgk_height - self.imageView.zgk_bottom;
    // 注意2:重寫layoutSubviews時最好,x,y,width,height都重寫一遍
    // 后面兩句雖然沒有改,但是也要補上,不然會出錯
    self.titleLabel.zgk_x = 0;
    self.titleLabel.zgk_width = self.zgk_width;
    
}
@end

3.九宮格的分割線實現(xiàn)(3種方案)

  • 1.用寬度為1~2的UIView添加到footerview
  • 2.每列的按鈕寬度width-1,但是最后一列不減,用底色來形成分割線
  • 3.通過美工做的背景四角方框圖片來實現(xiàn)
@implementation ZGKMeFooterviewButton
// 一次性初始化設(shè)置,寫在initWithFrame,xib寫在awakeFromNib,tableViewCell寫在initWithStyle
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        
        // 設(shè)置按鈕標(biāo)題顏色
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        // 注意:設(shè)置按鈕的背景圖片作為分隔線
        [self setBackgroundImage:[UIImage imageNamed:@"mainCellBackground"] forState:UIControlStateNormal];
        
    }
    
    return self;
}

4.關(guān)于九宮格按鈕的處理

    // 3.創(chuàng)建按鈕并設(shè)置模型數(shù)據(jù)
    // 注意1: 為了美觀,添加3個空白按鈕
    for (int index = 0; index < count + 3; index ++) {
//        NSLog(@"index = %d", index);
        ZGKMeFooterviewButton *btn = [ZGKMeFooterviewButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:btn];
        
        // 設(shè)置btn的frame
        btn.zgk_x = (index % maxColsCount) * maxWidth;
        btn.zgk_y = (index / maxColsCount) * maxHeight;
        btn.zgk_width = maxWidth;
        btn.zgk_height = maxHeight;
        
        // 注意2:給按鈕添加背景顏色,區(qū)別于footerview的背景顏色
        btn.backgroundColor = [UIColor whiteColor];
        
        
        // 4.拿到模型數(shù)據(jù),并賦值()
        // 注意3: 判斷如果超過了count個btn,則模型為nil
        ZGKMeSquareModel *square = index >= 33 ? nil : squares[index];
        
        [btn setTitle:square.name forState:UIControlStateNormal];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容