应用程序之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 ...
随机推荐
- JMeter 性能测试进阶实战
课程简介 本课程制作的主要目的是为了让大家快速上手 JMeter,期间穿插了大量主流项目中用到的技术,以及结合当今主流微服务技术提供了测试 Dubbo 接口.Java 工程技术具体实施方案,注重实践. ...
- 雪人(snowman)
test1025 五子棋(fir) 依照题意模拟即可,先判是否合法,然后在判是否胜利 迷宫(maze) 折半搜素裸题 雪人(snowman) 二分+hash a1-b1=a2-b2=a3-b3 等价于 ...
- python优雅写法
在这篇文章中我将和大家分享一些真正有用的技巧和窍门,这些技巧和窍门你们之前可能并不知道.所以不浪费时间了,让我们直接来看看这些内容吧: 枚举 之前我们这样操作: 1 2 3 4 i = 0 for i ...
- JavaScript文本收缩展开 showdetail
原文发布时间为:2009-11-15 -- 来源于本人的百度文章 [由搬家工具导入] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
- 全面解析Vue.nextTick实现原理
vue中有一个较为特殊的API,nextTick.根据官方文档的解释,它可以在DOM更新完毕之后执行一个回调,用法如下: // 修改数据 vm.msg = 'Hello' // DOM 还没有更新 V ...
- spring一些总结
Spring中三种实例化bean的方法: 1)使用类构造器 <bean id="orderService" class="cn.itcast.OrderServ ...
- 【全局变量】mysql查看全局变量以及设置全局变量的值
1.查看mysql的所有全局变量的值 SHOW GLOBAL VARIABLES 或者 SHOW VARIABLES mysql有很多全局变量,包括系统的一些基本信息,以及mysql的一些基本配置都可 ...
- Codeforces Gym 101194C Mr. Panda and Strips(2016 EC-Final,区间DP预处理 + 枚举剪枝)
题目链接 2016 EC-Final 题意 现在要找到数列中连续两个子序列(没有公共部分).要求这两个子序列本身内部没有重复出现的数. 求这两个子序列的长度的和的最大值. 首先预处理一下.令$ ...
- Codeforces 586D Phillip and Trains(DP)
题目链接 Phillip and Trains 考虑相对位移. 每一轮人向右移动一格,再在竖直方向上移动0~1格,列车再向左移动两格. 这个过程相当于每一轮人向右移动一格,再在竖直方向上移动0~1格, ...
- apache url rewrite问题
apache RewriteEngine Your browser sent a request that this server could not understand http://www.ra ...