博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS Xib布局某些控件显示或隐藏<约束的修改>
阅读量:4492 次
发布时间:2019-06-08

本文共 2655 字,大约阅读时间需要 8 分钟。

对于这个问题使用Masonry是很好解决的。

注意:绿色的是label2,当indexpath.section % 2 == 0时,label2不存在。

关键代码如下:

if (indexPath.section % 2 == 0) {

        [cell.label2 mas_updateConstraints:^(MASConstraintMaker *make) {

            make.height.mas_equalTo(0);

        }];

    }

代码如下:

////  ViewController.m//  XibTestDemo////  Created by 思 彭 on 2017/10/19.//  Copyright © 2017年 思 彭. All rights reserved.//#import "ViewController.h"#import "TableViewCell.h"#import 
@interface ViewController ()
@property (nonatomic, strong) UITableView *tableView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self setUI];}#pragma mark - 设置界面- (void)setUI { self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.backgroundColor = [UIColor clearColor]; self.tableView.tableFooterView = [[UIView alloc]init]; // 注册cell [self.tableView registerNib:[UINib nibWithNibName: NSStringFromClass([TableViewCell class]) bundle:nil] forCellReuseIdentifier:@"TableViewCell"]; // 行高// self.tableView.rowHeight = UITableViewAutomaticDimension;// self.tableView.estimatedRowHeight = 100; [self.view addSubview: self.tableView]; self.tableView.estimatedRowHeight = 0; self.tableView.estimatedSectionHeaderHeight = 0; self.tableView.estimatedSectionFooterHeight = 0;}#pragma mark - UITableViewDataSource- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 10;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath]; if (indexPath.section % 2 == 0) { [cell.label2 mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(0); }]; } return cell;}#pragma mark - UITableViewDelegate- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.001f;}- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 10;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section % 2 == 0) { return 60; } return 92;}@end

 Demo地址: https://github.com/PengSiSi/XibDemo

转载于:https://www.cnblogs.com/pengsi/p/7693180.html

你可能感兴趣的文章
mysql 分组之后统计记录条数
查看>>
New STL Algorithms That Will Make A More Productive Developer
查看>>
js 对象 浅拷贝 和 深拷贝
查看>>
初识 python
查看>>
PCL Examples
查看>>
spring boot
查看>>
浏览器URL传参最大长度问题
查看>>
学习进度条
查看>>
Linux crontab 定时任务详解
查看>>
string成员函数
查看>>
onSaveInstanceState()方法问题
查看>>
[转]CocoaChina上一位工程师整理的开发经验(非常nice)
查看>>
大数据时代侦查机制有哪些改变
查看>>
雷林鹏分享:jQuery EasyUI 菜单与按钮 - 创建链接按钮
查看>>
Apache Traffic Server服务搭建
查看>>
poj1990两个树状数组
查看>>
学习python-day1
查看>>
Zend_Db_Table->insert ()和zend_db_adapter::insert方法返回值不同
查看>>
递归问题
查看>>
Hyperledger下子项目
查看>>