IOS xib在tableview上的簡單應用(通過xib自訂cell),tableviewxib

來源:互聯網
上載者:User

IOS xib在tableview上的簡單應用(通過xib自訂cell),tableviewxib

UITableView是一種常用的UI控制項,在實際開發中,由於原生api的局限,自訂UITableViewCell十分重要,自訂cell可以通過代碼,也可以通過xib。

這篇隨筆介紹的是通過xib自訂cell。

首先通過gif介紹如何建立xib。

然後實現代碼部分,要注意的是實現代碼的同時要使代碼與xib相關聯。-

下面便是代碼,一些解釋我在代碼中注釋了。

ViewController.m

////  ViewController.m//  CX-Xib在tableView中的簡單應用////  Created by ma c on 16/3/18.//  Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "CXTableViewCell.h"static NSString * identifier = @"cxCellID";@interface ViewController()<UITableViewDataSource,UITableViewDelegate>@property (nonatomic, strong) UITableView * tableView;@end@implementation ViewController#pragma mark - set_and_get-(UITableView *)tableView{        if (!_tableView) {                _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, CGRectGetWidth(self.view.frame), 300) style:UITableViewStylePlain];                _tableView.delegate = self;                _tableView.dataSource = self;                _tableView.rowHeight = 100;                [_tableView registerNib:[UINib nibWithNibName:@"tableViewCellXib" bundle:nil] forCellReuseIdentifier:identifier];            }    return _tableView;}#pragma mark - life- (void)viewDidLoad {    [super viewDidLoad];        [self.view addSubview:self.tableView];}#pragma mark - deleDate-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return 1;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{        CXTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];        cell = [[[UINib nibWithNibName:@"tableViewCellXib" bundle:nil]instantiateWithOwner:self options:nil]lastObject];        return cell;    }@end

 CXTableViewCell.m

////  CXTableViewCell.m//  CX-Xib在tableView中的簡單應用////  Created by ma c on 16/3/18.//  Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "CXTableViewCell.h"@interface CXTableViewCell ()//這裡要先寫空間,然後把xib上的控制項和代碼相連@property (nonatomic, weak)IBOutlet UILabel * upLabel;@property (nonatomic, weak)IBOutlet UILabel * downLable;@property (nonatomic, weak)IBOutlet UIImageView * CXimageView;@end@implementation CXTableViewCell-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self) {                //不要把控制項add到view上        //add到contentView才是你最正確的選擇        [self.contentView addSubview:self.CXimageView];                [self.contentView addSubview:self.upLabel];                [self.contentView addSubview:self.downLable];    }    return self;}//採用xib自訂cell xib上的資訊要放在這裡更新- (void)awakeFromNib {     self.CXimageView.image = [UIImage imageNamed:@"caishen.jpg"];    self.upLabel.text = @"恭喜發財";    self.downLable.text = @"財源廣進";    }- (void)setSelected:(BOOL)selected animated:(BOOL)animated {    [super setSelected:selected animated:animated];}@end

在實際開發中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.