• 效果展示
  • 结构分析
  • 代码实现

一、效果展示

二、结构分析

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的更多相关文章

  1. IOS xib在tableview上的简单应用(通过xib自定义cell)

    UITableView是一种常用的UI控件,在实际开发中,由于原生api的局限,自定义UITableViewCell十分重要,自定义cell可以通过代码,也可以通过xib. 这篇随笔介绍的是通过xib ...

  2. iOS深入学习(UITableView系列4:使用xib自定义cell)

    可以通过继承UITableViewCell重新自定义cell,可以像下面一样通过代码来自定义cell,但是手写代码总是很浪费时间, ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  3. iOS 中使用 XIB 自定义cell 的两种方法 以及 编译出现常见 的错误 ++++(xcode6.0之后)

    一. 注册cell 1.创建自定义cell并勾选 xib :(勾选xib就会自动生成与cell文件关联的xib) 2.在 tableViewController里注册自定义Cell (或者遵守tabl ...

  4. iOS 中使用 XIB 自定义cell的两种方法以及编译出现常见 的错误 (xcode6.0之后)

    一. 注册cell 1.创建自定义cell并勾选 xib :(勾选xib就会自动生成与cell文件关联的xib) 2.在 tableViewController里注册自定义Cell (或者遵守tabl ...

  5. AJ学IOS(16)UI之XIB自定义Cell实现团购UI

    AJ分享,必须精品 先看效果图 自定义Cell 本次主要是自定义Cell的学习 实现自定义Cell主要有三种方法:按照使用的频繁度排序: XIB > 纯代码 > StoryBoard XI ...

  6. xib自定义cell代码规范

    // //  MJTgCell.m //  01-团购 // //  Created by apple on 14-4-1. //  Copyright (c) 2014年 itcast. All r ...

  7. iOS回顾笔记(08) -- 自定义Cell的类型和创建步骤总结

    iOS回顾笔记(08) -- 自定义Cell的类型和创建步骤总结 项目中我们常见的自定义cell主要分为两种 等高cell:如应用列表.功能列表 非等高cell:如微博列表.QQ聊天页面 下面对这 ...

  8. 自定义cell的一些知识

    1.要往cell里面添加一个自定义的子控件,都是添加到cell的contentView,不是添加到cell里面. 2.通过xib自定义cell * 添加tableView * 加载团购数据 * 新建x ...

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

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

随机推荐

  1. swiper单屏滚动

    .swiper-slide { overflow: auto; } 1. 排除某些屏,不滚动 var startScroll, touchStart, touchCurrent; var aSlide ...

  2. 1.docker学习

    Docker —— 从入门到实践 http://udn.yyuap.com/doc/docker_practice/introduction/index.html 非常详细的Docker学习教程 ht ...

  3. group by having执行顺序

    原文发布时间为:2009-07-28 -- 来源于本人的百度文章 [由搬家工具导入] 核心原理 where>group>having>order by 只有深入理解这些语句执行的过程 ...

  4. linux下的程序调试方法汇总

    搞电子都知道,电路不是焊接出来的,是调试出来的.程序员也一定认同,程序不是写出来的,是调试出来的.那么调试工具就显得尤为重要,linux作为笔者重要的开发平台,在linux中讨论调试工具主要是为那些入 ...

  5. ubuntu 15.10 64bit 下 steam无法启动

    首先查看steam日志,在/tmp/dumps/下,以“用户名_output.txt”命名. $ cat /tmp/dumps/liuxu_output.txt Running Steam on ub ...

  6. hdu 5056(尺取法思路题)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. BZOJ1801 [Ahoi2009]chess 中国象棋(DP, 计数)

    题目链接 [Ahoi2009]chess 中国象棋 设$f[i][j][k]$为前i行,$j$列放了1个棋子,$k$列放了2个棋子的方案数 分6种情况讨论,依次状态转移. #include <b ...

  8. Codeforces 586D Phillip and Trains(DP)

    题目链接 Phillip and Trains 考虑相对位移. 每一轮人向右移动一格,再在竖直方向上移动0~1格,列车再向左移动两格. 这个过程相当于每一轮人向右移动一格,再在竖直方向上移动0~1格, ...

  9. 深入V8引擎-Time核心方法之win篇(1)

    上一篇的源码看得十分无趣,官方文档跟黑心棉一样渣. 这一篇讲讲windows操作系统上的时间戳实现,由于类的声明,方法解释上一篇都贴过了,所以这次直接上对应版本的代码. windows与mac很不一样 ...

  10. POJ 1704 Georgia and Bob [博弈]

    题意:一个仅有一行的棋盘上,初始时有n个棋子,每人轮流移动棋子,每次只能移动一枚棋子,棋子在移动时只能向左移动,不能跨过别的棋子或跳出棋盘. 思路:这道题是一道nim游戏的巧妙变形,太棒了. 解决的思 ...