刷新tableView 保持頭部視圖 不變


1. 聲明全局變量

  UIView *headView; 表頭部View

    UIButton *financeBtn; //金融頭條Btn

    UIButton *companyNewsBtn;//公司新聞Btn

2.在viewDidLoad里面 

- (void)viewDidLoad {

    [super viewDidLoad];

    float screenWidth = kUIScreenWidth -70;

    financeBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [financeBtn setTitle:@"金融頭條" forState:UIControlStateNormal];

    [financeBtn.titleLabel setFont:[UIFont fontWithName:kPingFang_Medium size:14]];

    [financeBtn setFrame:CGRectMake(screenWidth/4-40, 12, 80, 25)];

    [financeBtn setTitleColor:[CommonFunctions colorWithHex:0x11a1b4] forState:UIControlStateNormal];

    [financeBtn setTag:222];

    [financeBtn addTarget:self action:@selector(companyNewsBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    companyNewsBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [companyNewsBtn setTitle:@"公司新聞" forState:UIControlStateNormal];

    [companyNewsBtn.titleLabel setFont:[UIFont fontWithName:kPingFang_Medium size:14]];

    [companyNewsBtn setFrame:CGRectMake((screenWidth/2)+(screenWidth/4-40), 12, 80, 25)];

    [companyNewsBtn setTitleColor:[CommonFunctions colorWithHex:0x999999] forState:UIControlStateNormal];

    [companyNewsBtn addTarget:self action:@selector(companyNewsBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    [companyNewsBtn setTag:333];

}

3.  區頭設置    在方法里面判斷不讓按鈕循環創建

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

     headView = [[UIView alloc] initWithFrame:CGRectMake(0, -1, kUIScreenWidth, 69)];

    headView.backgroundColor = [UIColor whiteColor];

   if(section == 0) {

        UIButton *mergeBtn = (UIButton *)[self.view viewWithTag:222];

        if (mergeBtn == nil) {

            [headView addSubview:financeBtn];

            [headView addSubview:companyNewsBtn];

        }

    }

return headView;

4. 實現按鈕的點擊方法就OK了

#pragma mark  金融頭條和公司新聞 Btn

- (void)companyNewsBtnClick:(UIButton *)sender {

   float screenWidth = kUIScreenWidth -70;

    UIButton *changeBtn = (UIButton *)[self.view viewWithTag:sender.tag];

    [changeBtn setTitleColor:[CommonFunctions colorWithHex:0x11a1b4] forState:UIControlStateNormal];

    if (sender.tag == 222) {

        NSLog(@"金融頭條");

        picImg.frame = CGRectMake(screenWidth/4-40, -1, 80, 4);

        [companyNewsBtn setTitleColor:[CommonFunctions colorWithHex:0x999999] forState:UIControlStateNormal];

    }else if (sender.tag == 333) {

        NSLog(@"公司新聞");

        picImg.frame = CGRectMake((screenWidth/2)+(screenWidth/4-40), -1, 80, 4);

        [financeBtn setTitleColor:[CommonFunctions colorWithHex:0x999999] forState:UIControlStateNormal];

    }

}

 

效果圖 如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM