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(, , CGRectGetWidth(self.view.frame), ) style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.rowHeight = ; [_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 ;
}
-(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的作用不仅仅局限于此,还用更多的功能等待你的发现。

IOS xib在tableview上的简单应用(通过xib自定义cell)的更多相关文章

  1. ios通知使用 书上案例 简单易懂

    /* The notification name */const NSString *ResultOfAppendingTwoStringsNotification =@"ResultOfA ...

  2. iOS UIKit:TableView之单元格配置(2)

    Table View是UITableView类的实例对象,其是使用节(section)来描述信息的一种滚动列表.但与普通的表格不同,tableView只有一行,且只能在垂直方向进行滚动.tableVi ...

  3. ios之UI中自定义cell

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  4. ios tableview 上加 textfiled

    ios tableview 上加 textfiled 首先附上我项目中用曾经用到的几张图  并说明一下我的用法: 图1: 图2: 图3: 心在你我说一下  我当初的实现 方法 ,希望能给你们一些  启 ...

  5. 史上最简单,一步集成侧滑(删除)菜单,高仿QQ、IOS。

    重要的话 开头说,not for the RecyclerView or ListView, for the Any ViewGroup. 本控件不依赖任何父布局,不是针对 RecyclerView. ...

  6. iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的 zhuang

    转载请注明出处. 今天在调试代码的时候,在tableviewcell上添加button,发现button快速点击的话,是看不出点击效果的,查找资料发现, ios7上UITableViewCell子层容 ...

  7. iOS xib中TableView创建的2种模式

    在xcode 5.0中 用xib编辑tableview有2种模式,见下图 其中,dynamic prototype 动态原型 表示tableview会询问它指定的 data source获取数据,如果 ...

  8. iOS学习之Table View的简单使用

    Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View ...

  9. ***iOS学习之Table View的简单使用和DEMO示例(共Plain普通+Grouped分组两种)

    Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View ...

随机推荐

  1. SmartJS 第一期(0.1)发布 - AOP三剑客

    隔了好久才终于又发布了一点东西,SmartJS是最近才开始搞的一个开源js库,目的是做一些比较有特点的事情(smartjs暂时也是依赖于jquery). SmartJS的内容规划比较多,也无法在短时间 ...

  2. MySQL批量SQL插入性能优化

    对于一些数据量较大的系统,数据库面临的问题除了查询效率低下,还有就是数据入库时间长.特别像报表系统,每天花费在数据导入上的时间可能会长达几个小时或十几个小时之久.因此,优化数据库插入性能是很有意义的. ...

  3. 代码演示 .NET 4.5 自带的 ReadonlyCollection 的使用

    代码如下: 1. using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  4. Git 版本管理

    应用情景1:添加文件(单个.多个.目录)到Git版本库,提交一个版本快照. 应用情景2:穿越历史回到过去,版本回退,从当前版本回退到之前提交过的某个版本上. 应用情景3:穿越历史过头了,版本回退多了, ...

  5. 使用Service.Stack客户端编写redis pub sub的方法

    pub相对简单 client.PublishMessage("channel", "msg");   sub有2种方法 方法1 var subscription ...

  6. set -x与set +x指令

    转载自:http://www.2cto.com/os/201304/205118.html 参考: http://blog.csdn.net/t0nsha/article/details/860688 ...

  7. 笔试测试开发题三道(python)

    笔试遇到的三道测试开发题,虽然都不难,但关键还是思路吧!我想在开发东西的时候应该具备的就是思路,有了思路尝试去写,或查相关文档或代码,在此基础上需要不断调整最终达到需求.思路又是在不断练习中获得的. ...

  8. NPTL 线程同步方式

    NPTL提供了互斥体 pthread_mutex_t 类型进行线程同步,防止由于多线程并发对全局变量造成的不正确操作.使用 pthread_mutext_t 对数据进行保护已经可以实现基本的数据同步, ...

  9. SQL Server 诊断查询-(1)

    Query #1 is Version Info. SQL and OS Version information for current instance SELECT @@SERVERNAME AS ...

  10. [Architect] Abp 框架原理解析(4) Validation

    本节目录 介绍 DataAnnotations ICustomValidate IShouldNormalize 实现Abp Validation 介绍 Abp中在Application层集成了val ...