ITeye博客

可展开的UITableView (附源码)

82934162 · 2012-08-28 11:44 · Objective-C · iOS · 移动开发 · iOS · iPhone · UITableView · uitableviewcell · 展开 · Markdown

由于工作需要,写了一个UITableView的子类,简单的实现了每个cell的展开和收缩的动画效果以及展开和收缩后的cell样式变化。这个效果也许你现在用不到,但是它在iOS上的效果确实很不错,也许以后你就会用到。分享给大家。给大家一个实际的效果: [img]http://dl.iteye.com/upload/attachment/0072/9698/d7df90bb-c368-3af0-b0db-89ec0f02fa62.png[/img] ExtensibleTableView.h [code="c++"] // // ExtensibleTableView.h // Wow // // Created by Boris Sun on 12-6-20. // Copyright (c) 2012年 adsit. All rights reserved. // #import @protocol ExtensibleTableViewDelegate @required //返回展开之后的cell - (UITableViewCell *)tableView:(UITableView *)tableView extendedCellForRowAtIndexPath:(NSIndexPath *)indexPath; //返回展开之后的cell的高度 - (CGFloat)tableView:(UITableView *)tableView extendedHeightForRowAtIndexPath:(NSIndexPath *)indexPath; @end @interface ExtensibleTableView : UITableView { //当前被展开的索引 NSIndexPath *currentIndexPath; id delegate_extend; } @property(nonatomic,retain)id delegate_extend; @property(nonatomic,retain)NSIndexPath *currentIndexPath; //将indexPath对应的row展开 - (void)extendCellAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated goToTop:(BOOL)goToTop; //将展开的cell收起 - (void)shrinkCellWithAnimated:(BOOL)animated; //查看传来的索引和当前被选中索引是否相同 - (BOOL)isEqualToSelectedIndexPath:(NSIndexPath *)indexPath; @end [/code] ExtensibleTableView.m [code="c"] // // ExtensibleTableView.m // Wow // // Created by Boris Sun on 12-6-20. // Copyright (c) 2012年 adsit. All rights reserved. // #import "ExtensibleTableView.h" @implementation ExtensibleTableView @synthesize delegate_extend; @synthesize currentIndexPath; - (id)init { currentIndexPath = nil; return [super init]; } //重写设置代理的方法,使为UITableView设置代理时,将子类的delegate_extend同样设置 - (void)setDelegate:(id)delegate { self.delegate_extend = delegate; [super setDelegate:delegate]; } /* 将indexPath对应的row展开 params: animated:是否要动画效果 goToTop:展开后是否让到被展开的cell滚动到顶部 */ - (void)extendCellAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated goToTop:(BOOL)goToTop { NSLog(@"debug 2"); //被取消选中的行的索引 NSIndexPath *unselectedIndex = [NSIndexPath indexPathForRow:[currentIndexPath row] inSection:[currentIndexPath section]]; //要刷新的index的集合 NSMutableArray *array1 = [[NSMutableArray alloc]init]; //若当前index不为空 if(currentIndexPath) { //被取消选中的行的索引 [array1 addObject:unselectedIndex]; } //若当前选中的行和入参的选中行不相同,说明用户点击的不是已经展开的cell if(![self isEqualToSelectedIndexPath:indexPath]) { //被选中的行的索引 [array1 addObject:indexPath]; } //将当前被选中的索引重新赋值 currentIndexPath = indexPath; if(animated) { [self reloadRowsAtIndexPaths:array1 withRowAnimation:UITableViewRowAnimationFade]; } else { [self reloadRowsAtIndexPaths:array1 withRowAnimation:UITableViewRowAnimationNone]; } if(goToTop) { //tableview滚动到新选中的行的高度 [self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; } } //将展开的cell收起 - (void)shrinkCellWithAnimated:(BOOL)animated { //要刷新的index的集合 NSMutableArray *array1 = [[NSMutableArray alloc]init]; if(currentIndexPath) { //当前展开的cell的索引 [array1 addObject:currentIndexPath]; //将当前展开的cell的索引设为空 currentIndexPath = nil; [self reloadRowsAtIndexPaths:array1 withRowAnimation:UITableViewRowAnimationFade]; } } //查看传来的索引和当前被选中索引是否相同 - (BOOL)isEqualToSelectedIndexPath:(NSIndexPath *)indexPath { if(currentIndexPath) { return ([currentIndexPath row] == [indexPath row]) && ([currentIndexPath section] == [indexPath section]); } return NO; } /* 重写了这个方法,却无效,因为这个方法总在didSelect之前调用,很奇怪。因为无法重写该方法,所以ExtensibleTableView不算完善,因为还有额外的代码需要在heightForRowAtIndexPath和cellForRowAtIndexPath中。哪个找到完善的方法后希望可以与qq82934162联系或者在http://borissun.iteye.com来留言 */ //- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath //{ // if([currentIndexPath row] == [indexPath row]) // { // return [self.delegate_extend tableView:self extendedCellForRowAtIndexPath:indexPath]; // } // return [super cellForRowAtIndexPath:indexPath]; //} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if([currentIndexPath row] == [indexPath row]) { return [self.delegate_extend tableView:self extendedHeightForRowAtIndexPath:indexPath]; } return [super rowHeight]; } @end [/code] 将这2个文件放到proj之后,要设置delegate_extend并且实现 //返回展开之后的cell - (UITableViewCell *)tableView:(UITableView *)tableView extendedCellForRowAtIndexPath:(NSIndexPath *)indexPath; //返回展开之后的cell的高度 - (CGFloat)tableView:(UITableView *)tableView extendedHeightForRowAtIndexPath:(NSIndexPath *)indexPath; 2个方法。 还有一点不合理的地方,我试着去解决,但是最终未果=。=! [code="c"] - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //若当前行被选中,则返回展开的cell if([tableView_ isEqualToSelectedIndexPath:indexPath]) { return [self tableView:tableView extendedCellForRowAtIndexPath:indexPath]; } ... }[/code] 这里要先判断当前行是否被选中,若被选中则调用extendedCellForRowAtIndexPath方法。因为我试着重写UITableView的- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath方法。试图在这个方法里做上边的事情,可是这个方法总是在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法之前被调用,因此没有达到预期的目标。 希望各位如果下载了源码,解决了这个问题的话,可以回到这里给我留言或者联系qq82934162. 以下是一个简单的demo源码: [url]http://dl.iteye.com/topics/download/4f242bbb-8004-3352-9604-b1211b7562df[/url]

评论(5)

  1. #5 楼 · jjhsing · 2013-01-25 13:17

    佩服。。。。。

  2. #4 楼 · 82934162 · 2012-10-31 19:37

    东方皓 writes:

    cellForRowAtIndexPath 一直不都是在 didSelect 之前调用吗?难道在之后调用吗?

    sorry,我写错了,我想写的是在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath之后。因此无法让被展开的cell直接在- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath是返回。。 谢谢哈

  3. #3 楼 · 东方皓 · 2012-10-17 10:23

    cellForRowAtIndexPath 一直不都是在 didSelect 之前调用吗?难道在之后调用吗?

  4. #2 楼 · wangjian1000096 · 2012-09-21 19:48

    楼主,点击事件后第一行会抖动诶

  5. #1 楼 · thinmonkey · 2012-08-28 13:45

    只有想不到的,没有做不到的,楼主很牛

评论功能维护中,当前仅展示历史评论。