美文网首页
UITableView使用,自定义cell,表头,表尾

UITableView使用,自定义cell,表头,表尾

作者: ly_chee_ | 来源:发表于2020-06-15 10:43 被阅读0次

    初始化cell

    UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, 320,500 ) style:UITableViewStyleGrouped];

    myTableView.separatorStyle = UITableViewCellAccessoryNone;//去掉下划线

    [myTableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"UITableViewCell"];

    //自定义cell
    //[myTableView registerClass:SquareImageCell.class forCellReuseIdentifier: @"SquareImageCell"];

    myTableView.dataSource = self;

    myTableView.delegate = self;

    [self.view addSubview:myTableView];

    代理中方法

    #pragma mark- UITableView代理

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

        return 2;

    }

    - (NSInteger)tableView:(nonnullUITableView*)tableViewnumberOfRowsInSection:(NSInteger)section {

        return 4;

    }

    - (CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{

        return 50;

    }

    - (nonnullUITableViewCell*)tableView:(nonnullUITableView*)tableViewcellForRowAtIndexPath:(nonnullNSIndexPath*)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;     //设置点击效果

    //自定义cell
    //SquareImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SquareImageCell" forIndexPath:indexPath];
    //cell.model=_modelArr[indexPath.row];

      returncell;

    }

    设置表头

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

     if(section ==0) {

    return 100;

    }else{

    return 0.1;

    }

    }

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

     if(section ==0) {//自定义表头

     MineHeaderView * headerView = [[MineHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];

     returnheaderView;

    }else{

     return [[UIView alloc] init ];

       }

    }

    设置表尾,同表头

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

     return 0.1;

    }

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

     return [[UIView alloc] init ];

    }

    #pragma mark - cell点击方法

    - (void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath {

        [tableViewdeselectRowAtIndexPath:indexPath animated:YES];

    }

    相关文章

      网友评论

          本文标题:UITableView使用,自定义cell,表头,表尾

          本文链接:https://www.haomeiwen.com/subject/kelcxktx.html