- 浏览: 53844 次
- 性别:
- 来自: 北京
晚年也过了,新的一年真正来临了,迎着太阳向着远方,来冒个泡
之前看到有花香太奇的九宫格,纯button实现的,感觉table更好用,就写了tableviewcell的,不是很难,主要还是view多了,怎么区分的问题,先上代码吧
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ int row=indexPath.row; NSString *indentifier=@"cell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:indentifier]; if(cell==nil){ cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier]; int width=(1024-320-itemNumPerRow*10)/itemNumPerRow; for(int i=0;i<itemNumPerRow;i++){ MediaView *itemView=[[MediaView alloc] initWithFrame:CGRectMake(i*width+10, 0, width, RowHeight)]; itemView.backgroundColor=[UIColor clearColor]; itemView.delegate=self; [itemView setTag:i+1]; itemView.mediaTag=row*itemNumPerRow+i+1; itemView.mediaImage=[UIImage imageNamed:@"aaa.png"]; [cell.contentView addSubview:itemView]; } } else{ for(int i=0;i<itemNumPerRow;i++){ MediaView *itemView=(MediaView *)[cell.contentView viewWithTag:i+1]; itemView.mediaTag=row*itemNumPerRow+i+1; itemView.mediaImage=[UIImage imageNamed:@"aaa.png"]; } } cell.selectionStyle=UITableViewCellSelectionStyleNone; return cell; }
MediaView是自定义的view,重写了一下draw函数,复杂的话还可以加入响应的按钮和delegate之类的
#import <UIKit/UIKit.h> #define MarginLeft 10 #define MarginRight 10 #define MarginTop 10 #define MarginBottom 35 @interface MediaView : UIView { UIImage *mediaImage; int mediaTag; } @property int mediaTag; #import "MediaView.h" @implementation MediaView - (void)drawRect:(CGRect)rect { // Drawing code. int imageWidth=self.frame.size.width-MarginLeft-MarginRight; int imageHeight=self.frame.size.height-MarginTop-MarginBottom; CGRect imageRect=CGRectMake(MarginLeft, MarginTop, imageWidth, imageHeight); [self.mediaImage drawInRect:imageRect]; CGRect textRect=CGRectMake(MarginLeft, self.frame.size.height-MarginBottom, imageWidth, MarginBottom); [[UIColor blueColor] set]; [@"test" drawInRect:textRect withFont:[UIFont systemFontOfSize:15]]; }
我一直习惯用if--else来填充cell,if里面初始化各种view的UI,并且设置相对固定的tag,else里面根据tag找到重用的控件,动态填充数据,动态的部分是用mediaTag来实现的
PS:有人问过有木有demo,从项目中抽了出来
- NineCellDemo.zip (472.9 KB)
- 下载次数: 96
发表评论
-
symbolicate小记
2014-05-22 16:46 442日志分析少不了symbolicate,一直都看的这 ... -
内存这玩意-1
2014-04-17 22:00 541来拔点草 今天看到个autorel ... -
优化table的图片加载
2013-06-14 10:08 687table的优化,貌似都说烂了,面试必考,网上也各种 ... -
拍照得到正确尺寸图片
2013-06-14 09:44 1658慢慢适应新环境,发点东西。 拍照有三条路子 ... -
最近知识点 1
2013-05-02 23:41 712今天是个好日子,入住新地方,深夜发帖纪念一下^^ ... -
Block再探
2013-04-13 11:18 904最近面试碰到一 ... -
NSSet的查找
2013-04-13 10:35 794看<Pro.iOS.Apps.Performa ... -
找工作前 自我总结一下
2013-03-04 19:21 1373KVC: 1.key-value-coiding 最 ... -
ASIHTTPRequest中的block
2012-12-24 16:38 1763先感慨一下,好深的坑... ASI很好用,特别 ... -
重写UIView 滑动手势的问题
2012-11-29 18:25 2685今天做个功能:tableview中,在某个cell上左右滑 ... -
dispatch小记
2012-11-26 21:13 837GCD大名鼎鼎,也经常用,不过大多是copy居多, ... -
transition小问题
2012-11-21 14:22 640朋友问的,发现transition在remove/add ... -
IOS 点滴 4
2012-08-30 14:19 667继续滴水 1.双缓冲 http://blo ... -
IOS 点滴 3
2012-08-25 12:38 693打球归来,发一个 1.关于性能 http: ... -
IOS 点滴 2
2012-08-23 19:41 555再来一篇,嘿嘿 1.sin函数 http:// ... -
IOS 点滴 1
2012-08-23 19:13 911好久没来,blog都长草了,囧,一直以来都觉得iteye这 ... -
copy/assgin小记
2011-12-03 11:21 1500看网上的帖子,总觉得copy的说明偏于简单了,对后来人伤 ... -
类似sina微博cell右下角的图片效果
2011-10-09 14:05 1346昨天看了sina微博,看到cell右下角的图片,就想 ... -
CALayer小记1
2011-09-27 12:20 3035今天有闲情逸致,再来一篇 1.contents填充 ... -
获取本地IP
2011-09-27 12:02 823再来一篇,获取本地IP,三种方法: (1)在Da ...
相关推荐
平常中所用的tableViewCell,左滑后,编辑状态只有一个删除按钮,现在很多APP上面都自定义实现了cell侧滑自定义编辑btn,比如qq,微信侧滑后有“消息置顶”、“标记为未读”等。 所以仿qq侧滑cell显示工具按钮写了...
自定义符合要求饿到cell,让你的tableview更加适用
swift简单的自定义tableViewCell实现
iOS TableViewCell 组件,选择单元格,然后显示一个下拉的详情信息。
uitableviewcell 高度自适应代码demo,喜欢的可以看看,也可以一起讨论下,有什么更好的方法来解决这个问题。
tableviewcell的风格改变默认的选中效果蓝色,可自定义cell的选中背景色
用Swift语言实现了自定义tableViewCell。 包含了一个TableViewCell的基类以及自定义的tableViewCell类的实现和调用。
tableviewcell的移动、删除、添加
ios TableViewCell收缩、伸缩、缩放功能
在UITableViewCell里面加入自定义控件,本文以Lable为例,给读者提供一种动态控制Cell高度的思想,由这种思想出发,读者可以以此来动态控制cell的高度。
用于tableViewCell 和 collectionViewCell 的移动 如发现问题请发邮件至ranheran@sohu.com,我会尽快修正
用Swift实现的简单自定义TableViewCell
tableViewCell图片lazyload 图片延迟加载
简单的tableViewCell的展开和收起,仿好友列表,自定义cell的内容。简单易懂,适合新手学习。
canFoldTabelViewCell 可折叠的TableViewcell
TableViewCell图片 在TableViewCell中显示照片的示例
tableViewCell按钮弹出视图
一款关于UITableView滑动时的动画,效果大概是上下滑动时,cell飞入或者淡入淡出这种效果,也可以根据这个效果的实现思路实现其他的效果,效果图没法上传,自己下载看下吧