1.文件结构:

2.

先创建一个xib文件,删除原有的view,添加一个TableViewCell控件。

3.ModelTableViewController.m文件

 #import "ModelTableViewController.h"
#import "Cell.h" @interface ModelTableViewController () @end @implementation ModelTableViewController static NSString *cellIdentifier = @"Cell"; - (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad]; // Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
UINib *nib = [UINib nibWithNibName:@"Cell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:cellIdentifier]; } - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } // Configure the cell... return cell;
}

注意:自定义的cell都需要注册,

此处注册Identifier时用xib文件注册

UINib *nib = [UINib nibWithNibName:@"Cell" bundle:nil];

[self.tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];

如果是自定义的Cell类,那么用这个函数:- (void)registerClass:forCellWithReuseIdentifier:注册。

这样就可以用了。

在自定义的xib文件中,如果Cell中有其它控件,如:UIButton等,

如果想在TableViewController中获取此button,那么不要通过连线Action,这样会出错。

应该通过以下方式:

1.先设置控件的tag值

2.再在datasource函数中取出,设置控件响应函数

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

中添加

UIButton *deleteButton = (UIButton *)[cell viewWithTag:1];

[deleteButton addTarget:self action:@selector(deleteModel:) forControlEvents:UIControlEventTouchUpInside];

这样不会出错。

用xib自定义UITableViewCell的更多相关文章

  1. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  2. 新手教程之使用Xib自定义UITableViewCell

    新手教程之使用Xib自定义UITableViewCell 前言 首先:什么是UITableView?看图 其次:什么是cell? 然后:为什么要自定cell,UITableView不是自带的有cell ...

  3. 【转】iOS 通过xib自定义UITableViewCell【原创】

    原文网址:http://blog.it985.com/9683.html 在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观.并且要不停的调整位置,字体什么的.这时, ...

  4. 通过xib自定义UITableViewCell

    通过xib自定义UITableViewCell 一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是pro ...

  5. 【swift学习笔记】三.使用xib自定义UITableViewCell

    使用xib自定义tableviewCell看一下效果图 1.自定义列 新建一个xib文件 carTblCell,拖放一个UITableViewCell,再拖放一个图片和一个文本框到tableviewc ...

  6. 用xib自定义UITableViewCell的注意事项——重用

    问题的提出: 有时候我们经常需要自定义tableView的cell,当cell里面的布局较为复杂时往往舍弃纯代码的方式而改用xib的方式进行自定义.当我们用纯代码的方式布局cell时,往往会在cell ...

  7. IOS学习之路七(通过xib自定义UITableViewCell)

    一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是product name是:UITableViewCe ...

  8. 【转】自定义UITableViewCell(registerNib: 与 registerClass: 的区别)

    自定义UITableViewCell大致有两类方法: 使用nib 1.xib中指定cell的Class为自定义cell类型(注意不是设置File's Owner的class) 2.调用 tableVi ...

  9. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

随机推荐

  1. Lanterns

    Lanterns 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/B 题目: Description ...

  2. win10添加打印机--无法访问指定设备,路径或文件。。

    win10添加打印机无法访问指定设备,路径或文件..后来发现很多按钮点击多说无法访问指定设备,路径或文件.. 解决添加打印机问题: 在搜索栏中搜索:print (从这里添加) 彻底解决: 添加环境变量 ...

  3. thinkphp类的调用

    1.在controller下新建一个类,类的名称必须按照tp的规范来写. 2.在需要调用的类中,只需new一下被调用的类名. $t=new DataController(); $t->m();

  4. event事件学习小节

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 汇编基础知识之二debug的使用

    DEBUG的使用 (要在win32位习题下进行,win7 64位需要安装DosBox和debug这2个软件): 1:win64位下debug的使用教程: 下载debug.exe,这里我把debug放在 ...

  6. Hibernate查询 Query Language

    1,Native SQL ->HQL->EJBQL->QBC(Query By Cretira)->QBE(Query By Example) 此排列是根据可实现功能大小排序.

  7. Linux磁盘及文件系统管理

    在LINUX中我们知道一个很重要的概念,那就是"一切皆文件",这里的一切表示所有在LINUX系统的对象,自然也包括了LINUX中的硬盘设备.在LINUX中所有设备都被抽象成一个文件 ...

  8. Mysql 性能调优之Memory 计算

    最近在做mariadb 数据库性能调优时发现,配置文件影响着整个数据库的性能的百分之80(这么说不为过),现在就我出现的问题来分析. 在压测mariadb时,tail 日志.发现压测到一半 ,数据库会 ...

  9. 栈的C++实现(数组)——创建-push-pop-top-清空栈-处理栈

    今天学习了利用数组方式的栈的C++实现,这种方式跟指针实现有很多不一样的地方: 栈的指针实现,栈的创建申请头结点,push需要申请新的结点,pop释放结点,这些结点都放在第一个位置,top时,S-&g ...

  10. 局域网访问本地localhost页面

    1.关闭防火墙 2.cmd - ipconfig - IPv4 地址 . . . . . . . . . . . . : 192.168.0.34 如果链接的是wifi,请右键确保手机和电脑链接同一个 ...