IOS xib在tableview上的简单应用(通过xib自定义cell)
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)的更多相关文章
- ios通知使用 书上案例 简单易懂
/* The notification name */const NSString *ResultOfAppendingTwoStringsNotification =@"ResultOfA ...
- iOS UIKit:TableView之单元格配置(2)
Table View是UITableView类的实例对象,其是使用节(section)来描述信息的一种滚动列表.但与普通的表格不同,tableView只有一行,且只能在垂直方向进行滚动.tableVi ...
- ios之UI中自定义cell
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- ios tableview 上加 textfiled
ios tableview 上加 textfiled 首先附上我项目中用曾经用到的几张图 并说明一下我的用法: 图1: 图2: 图3: 心在你我说一下 我当初的实现 方法 ,希望能给你们一些 启 ...
- 史上最简单,一步集成侧滑(删除)菜单,高仿QQ、IOS。
重要的话 开头说,not for the RecyclerView or ListView, for the Any ViewGroup. 本控件不依赖任何父布局,不是针对 RecyclerView. ...
- iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的 zhuang
转载请注明出处. 今天在调试代码的时候,在tableviewcell上添加button,发现button快速点击的话,是看不出点击效果的,查找资料发现, ios7上UITableViewCell子层容 ...
- iOS xib中TableView创建的2种模式
在xcode 5.0中 用xib编辑tableview有2种模式,见下图 其中,dynamic prototype 动态原型 表示tableview会询问它指定的 data source获取数据,如果 ...
- iOS学习之Table View的简单使用
Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View ...
- ***iOS学习之Table View的简单使用和DEMO示例(共Plain普通+Grouped分组两种)
Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View ...
随机推荐
- log4net日志功能使用
早就想了解下log4net这个组件的使用,直至今日才有时间学习了一下,现在把学习的新的总结如下: (1).在项目中添加log4net.dll引用.说明:该版本是1.2.10.0 ,log4ne ...
- C# 类型基础
引言 本文之初的目的是讲述设计模式中的 Prototype(原型)模式,但是如果想较清楚地弄明白这个模式,需要了解对象克隆(Object Clone),Clone其实也就是对象复制.复制又分为了浅度复 ...
- 菜鸟学Windows Phone 8开发(3)——布局和事件基础
本系列文章来源MSDN的 面向完全新手的 Windows Phone 8 开发 本文地址:http://channel9.msdn.com/Series/Windows-Phone-8-Develo ...
- ruby -- 问题解决(六)link_to to destroy not working
在Rails的development环境下开发时,使用link_to进行destroy时顺利进行,最近将环境配置成production时,使用link_to,结果not working 代码如下: & ...
- linux 文件与进程
1. linux查看一个文件正被那些程序使用 fuser -hUsage: fuser [-fMuvw] [-a|-s] [-4|-6] [-c|-m|-n SPACE] [-k [-i] [-SIG ...
- [Logstash]使用详解
Logstash是一款轻量级的日志搜集处理框架,可以方便的把分散的.多样化的日志搜集起来,并进行自定义的处理,然后传输到指定的位置,比如某个服务器或者文件. 本文针对官方文档进行翻译以及实践,希望有更 ...
- 初涉SQL Server性能问题(2/4):列出等待资源的会话
在初涉SQL Server性能问题(1/4)里,我们知道了如何快速检查服务器实例上正运行的任务数和IO等待的任务数.这个是轻量级的脚本,不会给服务器造成任何压力,即使服务器在高负荷下,也可以正常获得结 ...
- 透过WebGL 3D看动画Easing函数本质
50年前的这个月诞生了BASIC这门计算机语言,回想起自己喜欢上图形界面这行,还得归功于当年在win98下用QBASIC照葫芦画瓢敲了一段绘制奥运五环的代码,当带色彩的奥运五环呈现在自己面前时我已知道 ...
- JS魔法堂:浏览器模式和文档模式怎么玩?
一.前言 从IE8开始引入了文档兼容模式的概念,作为开发人员的我们可以在开发人员工具中通过“浏览器模式”和“文档模式”(IE11开始改为“浏览器模式”改成更贴切的“用户代理字符串”)品味一番,它的出现 ...
- 查找表或其他对象在某个Server上的存在
EXEC sp_MSforeachdb 'use ? ; IF EXISTS(SELECT top 1 1 FROM sys.syscomments WHERE text LIKE ''%test% ...