用TableView写带特效的cell

效果:

源码地址:

https://github.com/YouXianMing/UI-Component-Collection

分析:

在UIScrollView中的代理中发送广播,然后在cell中接收广播

对每一个cell进行设置

对开发有利的一种小细节:

核心源码:

控制器源码

//
// ViewController.m
// TableView
//
// Created by XianMingYou on 15/4/9.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import "ViewController.h"
#import "DataCell.h" @interface ViewController ()<UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 数据源
self.dataSource = @[@"YouXianMing", @"Google",
@"iOS Developer", @"Android Developer", @"YouTube",
@"UI Delveloper", @"PS4 Player", @"XboxOne Player"]; // 初始化tableView
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableView registerClass:[DataCell class] forCellReuseIdentifier:DATA_CELL];
[self.view addSubview:self.tableView];
} - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// 发送广播
[[NSNotificationCenter defaultCenter] postNotificationName:DATA_CELL
object:@(scrollView.contentOffset.y)
userInfo:nil];
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSource.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DataCell *cell = [tableView dequeueReusableCellWithIdentifier:DATA_CELL];
cell.indexPath = indexPath;
cell.label.text = self.dataSource[indexPath.row]; return cell;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return CELL_HEIGHT;
} @end

cell源码

//
// DataCell.h
// TableView
//
// Created by XianMingYou on 15/4/9.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import <UIKit/UIKit.h> #define DATA_CELL @"DataCell"
#define CELL_HEIGHT (56.8f * 2) @interface DataCell : UITableViewCell @property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, strong) UILabel *label; @end
//
// DataCell.m
// TableView
//
// Created by XianMingYou on 15/4/9.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import "DataCell.h"
#import "UIView+SetRect.h" @interface DataCell ()
@property (nonatomic, strong) UIView *blackView;
@end @implementation DataCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { self.selectionStyle = UITableViewCellSelectionStyleNone; // 注册通知中心
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notificationEvent:)
name:DATA_CELL
object:nil]; // 构建子控件
[self buildViews];
} return self;
} - (void)buildViews {
self.label = [[UILabel alloc] initWithFrame:CGRectMake(, , , CELL_HEIGHT)];
self.label.font = [UIFont fontWithName:@"Avenir-BookOblique" size:.f];
[self addSubview:self.label]; self.blackView = [[UIView alloc] initWithFrame:CGRectMake( + , , , )];
self.blackView.backgroundColor = [UIColor blackColor];
[self addSubview:self.blackView];
} - (void)notificationEvent:(id)sender { NSDictionary *data = sender;
CGFloat offsetY = [[data valueForKey:@"object"] floatValue] - self.indexPath.row * CELL_HEIGHT; if (offsetY >= && offsetY <= CELL_HEIGHT) {
// 根据百分比计算
CGFloat percent = - offsetY / CELL_HEIGHT; // 设置值
self.label.alpha = percent;
self.blackView.x = + percent * ; } else if (offsetY >= - CELL_HEIGHT * && offsetY <= - CELL_HEIGHT * ) {
// 根据百分比计算
CGFloat percent = (offsetY + CELL_HEIGHT) / CELL_HEIGHT + ; // 设置值
self.label.alpha = percent;
self.blackView.x = + + ( - percent) * ;
} else {
// 复位
self.label.alpha = .f;
self.blackView.x = + ;
}
} - (void)dealloc {
// 移除通知中心
[[NSNotificationCenter defaultCenter] removeObserver:self
name:DATA_CELL
object:nil];
} @end

用TableView写带特效的cell的更多相关文章

  1. IOS Swift语言开发 tableView的重用以及自cell的自适应高度

    http://www.aichengxu.com/iOS/11143168.htm 一.准备数据 (这是一个元组,第一个元素为英雄的名字;第二个元素为英雄头像图片的名字,格式为.PNG,如果为其他的格 ...

  2. 127使用 TableView 自带的单元格样式实现好友列表,另外在单元格中添加辅助按钮

    类似的做法如之前这篇随笔:114自定义UITableViewCell(扩展知识:为UITableViewCell添加动画效果) 相比之下:自定义 UITableViewCell 的内容灵活,可根据需求 ...

  3. UITableVIew与UICollectionView带动画删除cell时崩溃的处理

    UITableVIew与UICollectionView带动画删除cell时崩溃的处理 -会崩溃的原因是因为没有处理好数据源与cell之间的协调关系- 效果: tableView的源码: ModelC ...

  4. python使用pyqt写带界面工具

    上篇介绍的使用python自带tkinter包,来写带界面的工具. 此篇介绍使用pyqt来开发测试工具. tkinter的好处是python官方自带,上手容易(但手写控件复杂),布局和摆放都不直观和容 ...

  5. [How to]使用自定义cell进行tableview的创建,适用于cell样式不发生变化的情况。

    1.简介 在tableview中又默认的cell格式,其中组织如下: <截取自官网文档> 最终的在页面上默认的cell也只能像上述那样的显示效果,如果这种要是无法满足我们的界面要求,那么我 ...

  6. 纯css写带小三角对话框

    在实际样式中经常会遇到要写类似对话框的样式,而这种样式往往会有一个小三角,如下所示: 那么如何用css写出来呢,其实很简单,先让父元素相对定位,然后运用css的伪类before或after.就可以写个 ...

  7. 【iOS开发-68】APP下载案例:利用tableView自带的cell布局+缓存池cell复用时注意button状态的检查

    (1)效果 (2)源码与资源下载 http://pan.baidu.com/s/1pJLo2PP (3)总结 --核心是利用UITableView里面自带的cell来制作样式同样的cell. 与之对应 ...

  8. iOS学习——tableview中带编辑功能的cell键盘弹出遮挡和收起问题解决

    最近在项目中经常用到UITableView中的cell中带有UITextField或UITextView的情况,然后在这种场景下,当我们点击屏幕较下方的cell进行编辑时,这时候键盘弹出来会出现遮挡待 ...

  9. iOS - (TableView中利用系统的 cell 设置 cell.textlabel 位置和大小)

    今天工作稍微的遇到了一点小小的难题,需求效果中 TableView cell 中的 Label 字体大小比原先系统中的要大些且 Label 位置不是在前面,而是在中间往后,对于这个问题我第一时间也是想 ...

随机推荐

  1. 【Express系列】第3篇——接入mysql

    通常来说,前后端分离的项目,前端负责界面渲染和操作型的业务逻辑,后端则负责数据存取和数据处理相关的业务逻辑. 既然设计数据,那就少不了数据库的使用.目前市面上流行着各种各样的数据库,这里不打算一一列举 ...

  2. js便签笔记(1)——说说HTMLCollection、NodeList以及NamedNodeMap

    介绍 在js的dom操作中,除了常用的document.html**Element之外,还有三个集合对象,即HTMLCollection.NodeList以及NamedNodeMap.试看以下操作: ...

  3. java学习-MD5消息摘要算法

    md5 属于hash算法一类,是不可逆的消息摘要算法.与对称加密和非对称加密算法不一样,不需要加密密钥. 注意: md5不是加密算法,只是将数据进行散列计算后生成一个唯一值的算法,没有加密密钥也没有解 ...

  4. 使用aapt解析apk,得到package内容

    $cmd = C('APPT_PREFIX'); exec(C('APPT_PREFIX') . UPLOAD_RES_PATH . $up_az, $out, $return); && ...

  5. Sharepoint Timer job问题汇总

    解决方案发布最好发布到GAC中,使用WebApplication很多时候会有问题.TimerJob并非是在IIS下运行,所以发布到wss目录下的dll不能使用. 如果解决方案中只有一个Timer Jo ...

  6. 浏览器Request Header和Response Header的内容

    1)请求(客户端->服务端[request])     GET(请求的方式) /newcoder/hello.html(请求的目标资源) HTTP/1.1(请求采用的协议和版本号)     Ac ...

  7. [javaSE] 集合框架(HashSet)

    Set:元素是无序,不可重复的 HaseSet:底层数据结构是哈希表 定义一个类Demo 获取Demo对象,system.out.println(demo),打印demo对象,Demo@xxxxxx ...

  8. 啰里吧嗦jvm

    一.为什么要了解jvm 有次做项目的时候,程序run起来的时候,总是报OutOfMemoryError,有老司机教我们用jconsole.exe看内存溢出问题 就是这货启动jconsole后,发现一个 ...

  9. spring MethodInterceptor方法拦截

    引用别的的:https://blog.csdn.net/u010739551/article/details/47754731 最近项目里加上了用户权限,有些操作需要登录,有些操作不需要,之前做项目做 ...

  10. UML 类图详解

    转载来源:http://blog.csdn.net/shift_wwx/article/details/79205187 可以参考:http://www.uml.org.cn/oobject/2012 ...