用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. ArrayDeque解析

    Queue接口 public abstract boolean add(E paramE); public abstract boolean offer(E paramE); // 加入元素 publ ...

  2. JavaScript文档对象模型

    文档对象模型(Document Object Model, DOM)是W3C提出的用于访问和修改文档的接口. JavaScript设计的初衷是为Web提供交互功能,它通过DOM接口来访问和修改文档. ...

  3. .net面试题[转载]

    1.简述private.protected.public.internal修饰符的访问权限. private:私有成员,在类的内部才可以访问. protected:保护成员,该类内部和继承类中可以访问 ...

  4. C# winfrom打印技术初探

    最近用到了winform去打印,网上查了一些资料,大概内容: 一 .首先有几个类 PageSetupDialog . PrintDialog .PrintDocument .PrintPreviewC ...

  5. 钉钉微应用接入钉钉免登陆配置记录。NET实现

    在这里记录一下我配置的钉钉接入微应用遇到的坑.搞了我几天天才调通.头皮发麻,现在梳理一下,以免别人也入坑. 1.钉钉接入主要要获取钉钉企业员工的ID,然后去自己的应用的数据库里进行匹配然后实现免登陆的 ...

  6. 撩课-Web大前端每天5道面试题-Day20

    1.vue生命周期的作用是什么? 它的生命周期中有多个事件钩子,让我们在控制整个Vue实例的过程时更容易形成好的逻辑. 2. Vue实现数据双向绑定的原理:Object.defineProperty( ...

  7. Eclipse的版本命名

    Eclipse自3.1开始使用木星的卫星作为版本名,例如: 木卫一:伊奥 lo木卫二:欧罗巴 Europa木卫三:伽倪墨得斯 Ganymede木卫四:卡利斯托 Callisto .... Eclips ...

  8. SSM+Redis+Shiro+Maven框架搭建及集成应用

    引文: 本文主要讲述项目框架搭建时的一些简单的使用配置,教你如何快速进行项目框架搭建. 技术: Spring+SpringMVC+Mybatis+Redis+Shiro+Maven          ...

  9. Mybatis中的jdbcType的作用

    使用MyBatis框架做更新操作时,在该字段需要更新的内容为空时,就会出现1111错误,也就是无效的列类型,这个时候你就要使用jdbcType.至于什么时候要使用到javaType我还没遇到过,而且我 ...

  10. mac,macbook 连接蓝牙耳机播放音乐断断续续

    个人的情况是, mac本连的网线,用的无线鼠标, 屋里80多号人都在用笔记本,应该也有好多开着无线的东西 解决方法: mac 或macbook 连接蓝牙耳机播放音乐断断续续的原因, 在网上找了好多方法 ...