iOS开发-UITableView自定义Cell
UITableView在iOS中开发的重要地位是毋庸置疑的,基本上应用中用到的比例是一半左右,而且大部分情况都是需要自定义单元格的,这样用户看到的App才能更有美感。之前写过UITableView的基本用法,如果对UITableView不是很熟悉可以参考本人之前的博客,因此很多UITableView的知识点就默认你已经熟悉了,先看下自定义实现的效果,这是自定义的Cell最后展现的效果:

自定义Cell
1.首先新建一个CustomCell.xib文件,方式如下:

2.新建一个继承自UITableViewCell的CustomTableViewCell类,需要重写initWithStyle方法:
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
self=[views firstObject];
}
return self;
}
3.CustomCell.xib中拖入一个UITableViewCell,然后和CustomTableViewCell关联:

关联之后CustomTableViewCell多了一个属性:
@property (weak, nonatomic) IBOutlet UILabel *title;
UITableView加载Cell
1.viewDidLoad初始化UITableView:
self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(10, 10,CGRectGetWidth(self.view.bounds)-20, CGRectGetHeight(self.view.bounds)-10)];
self.tableView.delegate=self;
self.tableView.dataSource=self;
[self.view addSubview:self.tableView];
2.初始化标题数组travelArr:
-(NSArray *)travelArr{
_travelArr=@[@"北京-清迈",@"北京-香港",@"北京-东京",@"北京-大阪",@"北京-新加坡",@"北京-维多利亚",@"北京-纽约",@"北京-夏威夷",@"北京-维多利亚",@"北京-柬埔寨"];
return _travelArr;
}
3.实现UITableViewDataSource中的方法:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.travelArr count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier=@"CustomCell";
CustomTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
cell=[[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.title.text=[self.travelArr objectAtIndex:indexPath.row];
return cell;
}
4.实现UITableViewDelegate中的方法:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}
简单的自定义大概就是如此了,如果有所收获,帮忙推荐下~
iOS开发-UITableView自定义Cell的更多相关文章
- 李洪强iOS开发之自定义cell的使用
第一步: 创建自定义cell类,继承自UItableVIewcell 第二步: 在sb中布局自己需要的视图控件并且将此cell与我刚刚创建的cell类进行关联.并且连线 第三步: 创建modle类, ...
- iOS开发 UITableView之cell
1.cell简介 UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行 U ...
- iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath(汇总)
iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath 首先分析有几种原因,以及相应的解决方法 1.UITableViewCell的userInterac ...
- iOS开发之自定义表情键盘(组件封装与自动布局)
下面的东西是编写自定义的表情键盘,话不多说,开门见山吧!下面主要用到的知识有MVC, iOS开发中的自动布局,自定义组件的封装与使用,Block回调,CoreData的使用.有的小伙伴可能会问写一个自 ...
- 详解iOS开发之自定义View
iOS开发之自定义View是本文要将介绍的内容,iOS SDK中的View是UIView,我们可以很方便的自定义一个View.创建一个 Window-based Application程序,在其中添加 ...
- iOS开发之自定义UITableView的cell
系统默认的UITableViewCell的每行都有横线(分隔符),就算没有数据也是如此,有时候我们想只在有数据的地方有下划线,可以去除下划线,然后在awarkFromNid方法中使用addsubvie ...
- iOS开发,UITableView相关问题
第一条:UITableViewCell 内容的设置 //文本放到最后 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.co ...
- iOS开发UITableView基本使用方法总结
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
随机推荐
- Fisher–Yates shuffle 洗牌算法(zz)
1,缘起 最近工作上遇到一个问题,即将一组数据,比如[A,B,C,D,E]其中的两个B,E按随机排列,其他的仍在原来的位置: 原始数组:[A,B,C,D,E] 随机字母:[B,D] 可能结果:[A,B ...
- BZOJ.2246.[SDOI2011]迷宫探险(DP 记忆化搜索 概率)
题目链接 求最大的存活概率,DP+记忆化. 用f[s][x][y][hp]表示在s状态,(x,y)点,血量为hp时的存活概率. s是个三进制数,记录每个陷阱无害/有害/未知. 转移时比较容易,主要是在 ...
- hdu 4438 第37届ACM/ICPC 天津赛区现场赛H题
题意:Alice和Bob两个人去打猎,有两种(只)猎物老虎和狼: 杀死老虎得分x,狼得分y: 如果两个人都选择同样的猎物,则Alice得分的概率是p,则Bob得分的概率是(1-p): 但是Alice事 ...
- BZOJ 3101: N皇后 构造
3101: N皇后 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3101 Description n*n的棋盘,在上面摆下n个皇后,使其 ...
- 使用 IntraWeb (11) - 基本控件之 TIWButton
所在单元及继承链: IWCompButton.TIWButton < TIWCustomControl < TIWBaseHTMLControl < TIWBaseControl & ...
- Data transfer from GPIO port to RAM buffer using DMA upon receiving a trigger signal on the timer capture input channel.
Data transfer from GPIO port to RAM buffer using DMA upon receiving a trigger signal on the timer ca ...
- STM32学习日志--使用DMA功能自动更新PWM的输出
/******************************************************************************* 编译环境: EWARM V5.30 硬 ...
- AngularJS报错:[$injector:unpr] Unknown provider: $templateRequestProvider
在页面中由上到下引用了: angular.js angular-route.js 创建model的时候也写明了依赖: var someApp = angular.module('someApp',[' ...
- Delphi 判断时间是否合法 -IsValidDateTime、IsValidDate、IsValidTime、IsValidDateDay
DateUtils.IsValidDateTimeDateUtils.IsValidDateDateUtils.IsValidTimeDateUtils.IsValidDateDayDateUtils ...
- java ffmpeg视频转码(自测通过)
import java.io.*; public class VideoTransfer { //ffmepg文件 安装目录 private static String ffmpeg = " ...