应用程序之Xib自定义Cell
- 效果展示
- 结构分析
- 代码实现
一、效果展示

二、结构分析
1⃣️首先我们让我们的控制器不再继承UIViewController,而是继承UITableViewController。这样就直接遵守了delegate协议、dataSource协议
2⃣️数据使用模型来加载
3⃣️自定义的Cell使用单独的类来管理
三、代码实现


//
// BookController.m
// 01-自定义cell04
//
// Created by apple on 14-4-9.
// Copyright (c) 2014年 apple. All rights reserved.
// #import "BookController.h"
#import "Book.h"
#import "BookCell.h" @interface BookController ()
{
NSMutableArray *books;
}
@end @implementation BookController - (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
books = [NSMutableArray array];
for (int i = ; i<; i++) {
Book *b = [Book bookWithName:[NSString stringWithFormat:@"iOS开发%d", i] price:arc4random_uniform()/10.0 + ];
[books addObject:b];
} } - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return books.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//加载的cell就是xib对应的BookCell对象
BookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:nil options:nil] lastObject]; } Book *book = books[indexPath.row];
cell.nameLabel.text = book.name;
cell.priceLable.text = [NSString stringWithFormat:@"¥%.1f", book.price]; UIButton *collect = cell.collectBtn;
[collect addTarget:self action:@selector(collectClick: event:) forControlEvents:UIControlEventTouchUpInside]; return cell;
} - (void)collectClick:(UIButton*)btn event:(UIEvent*)event
{ //NSLog(@"----%@", event);
UITableView *tableView = (UITableView *)self.view; // 获取所有的触摸点(UITouch对象,如果是单点触碰,就只有1个UITouch)
NSSet *touches = [event allTouches]; // 一个UITouch对象对应一根手指
UITouch *touch = [touches anyObject]; // 获取触摸点在UITableView上面的的位置
CGPoint position = [touch locationInView:tableView]; // 根据触摸位置 得到 对应的行号
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:position];
//NSLog(@"%d", indexPath.row); Book *book = books[indexPath.row];
NSLog(@"%@", book.name);
} #pragma mark - Table view delegate - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//取消选中蓝色
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
} @end
应用程序之Xib自定义Cell的更多相关文章
- IOS xib在tableview上的简单应用(通过xib自定义cell)
UITableView是一种常用的UI控件,在实际开发中,由于原生api的局限,自定义UITableViewCell十分重要,自定义cell可以通过代码,也可以通过xib. 这篇随笔介绍的是通过xib ...
- iOS深入学习(UITableView系列4:使用xib自定义cell)
可以通过继承UITableViewCell重新自定义cell,可以像下面一样通过代码来自定义cell,但是手写代码总是很浪费时间, ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- iOS 中使用 XIB 自定义cell 的两种方法 以及 编译出现常见 的错误 ++++(xcode6.0之后)
一. 注册cell 1.创建自定义cell并勾选 xib :(勾选xib就会自动生成与cell文件关联的xib) 2.在 tableViewController里注册自定义Cell (或者遵守tabl ...
- iOS 中使用 XIB 自定义cell的两种方法以及编译出现常见 的错误 (xcode6.0之后)
一. 注册cell 1.创建自定义cell并勾选 xib :(勾选xib就会自动生成与cell文件关联的xib) 2.在 tableViewController里注册自定义Cell (或者遵守tabl ...
- AJ学IOS(16)UI之XIB自定义Cell实现团购UI
AJ分享,必须精品 先看效果图 自定义Cell 本次主要是自定义Cell的学习 实现自定义Cell主要有三种方法:按照使用的频繁度排序: XIB > 纯代码 > StoryBoard XI ...
- xib自定义cell代码规范
// // MJTgCell.m // 01-团购 // // Created by apple on 14-4-1. // Copyright (c) 2014年 itcast. All r ...
- iOS回顾笔记(08) -- 自定义Cell的类型和创建步骤总结
iOS回顾笔记(08) -- 自定义Cell的类型和创建步骤总结 项目中我们常见的自定义cell主要分为两种 等高cell:如应用列表.功能列表 非等高cell:如微博列表.QQ聊天页面 下面对这 ...
- 自定义cell的一些知识
1.要往cell里面添加一个自定义的子控件,都是添加到cell的contentView,不是添加到cell里面. 2.通过xib自定义cell * 添加tableView * 加载团购数据 * 新建x ...
- 新手教程之使用Xib自定义UITableViewCell
新手教程之使用Xib自定义UITableViewCell 前言 首先:什么是UITableView?看图 其次:什么是cell? 然后:为什么要自定cell,UITableView不是自带的有cell ...
随机推荐
- 【WC2019笔记】模拟费用流算法
在一条数轴上,有 $n$ 只老鼠和 $m$ 个老鼠洞. Q1 每只老鼠都只能往左走,求所有老鼠都进洞的最小代价(代价就是所有老鼠走的距离和). 每个洞只能进一只老鼠. A1 一开始陈江伦老师没说每个洞 ...
- 关于fixed定位的一些错误看法纠正
之前由于一些误导,一直感觉fixed这个定位在ie8下面是会出现兼容问题的,今天发现这个想法太绝对了,它只是在ie7 8 的怪异模式下面会出现兼容问题 解决这个问题可以通过用absolute来模拟fi ...
- iOS资讯详情页实现—WebView和TableView混合使用(转)
iOS资讯详情页实现—WebView和TableView混合使用 如果要实现一个底部带有相关推荐和评论的资讯详情页,很自然会想到WebView和TableView嵌套使用的方案. 这个方案是WebVi ...
- 感谢Sylvia的技术支持
感谢Sylvia的技术支持,让Tabb有了生命力.
- HH去散步(bzoj 1875)
Description HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走,就是散步,就是在一定的时间 内,走过一定的距离. 但是同时HH又是个喜欢变化的人,所以他不会立刻沿着刚刚走来的路走回. 又因 ...
- BitmapData处理图像例子【逆反处理】
原文发布时间为:2009-01-16 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Collections.Generic;using Syste ...
- 阿里巴巴Java开发手册公开版(转)
1.不要嫌名字长 无论是方法,变量,还是函数的取名,不要嫌弃名称太长,只要能够表示清楚含义就可以了. 2.String[] args而不是String args[] 中括号是数组类型的一部分,数组定义 ...
- android基本控件学习-----ToggleButton&Switch
ToggleButton(开关按钮)和Switch(开关)讲解: 一.核心属性讲解: (1)ToggleButton textOn:按钮被选中的时候文字显示 textOff:按钮没有被选中的时候文字显 ...
- 例说linux内核与应用数据通信系列【转】
转自:http://blog.csdn.net/shallnet/article/details/47865169 版权声明:本文为博主原创文章,未经博主允许不得转载.如果您觉得文章对您有用,请点击文 ...
- Mysql同台机器主从配置
Mysql主从配置 安装主Mysql 安装前的准备 检查系统是否存在mysql用户 [root@test mysql]# less /etc/passwd | grep mysql mysql:x:5 ...