iOS Xib布局某些控制項顯示或隱藏<約束的修改>

來源:互聯網
上載者:User

標籤:constrain   atom   property   width   init   apple   mat   queue   idt   

對於這個問題使用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 <Masonry.h>@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>@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

iOS Xib布局某些控制項顯示或隱藏<約束的修改>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.