自定义的UITableViewController,返回时取消row选择
来源:互联网 发布:数据库like语句 编辑:程序博客网 时间:2024/05/18 00:31
一般来说,ViewController继承自UITableViewController是最方便的,因为已经有了很多默认的行为。比如会自动设置tableView属性,自动调用reloadData,自动支持滚动等。但是有的时候,需要自定义UITableView,所以相应的也需要自定义UITableViewController
比如创建一个UIView,上半部分是TableView,下半部分有一个自定义视图,这时候就需要自定义UIView和UIViewController
@interface SettingView : UIView@property UITableView *tableView;-(id) initWithController:(SettingViewController*)controller;@end
@implementation SettingView-(id) initWithController:(SettingViewController*)controller{ self = [super initWithFrame:[[UIScreen mainScreen] applicationFrame]]; if(self){ self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 468) style:UITableViewStylePlain]; self.tableView.dataSource = controller; self.tableView.delegate = controller; [self.tableView setScrollEnabled:NO]; self.tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10); self.tableView.tableFooterView = [[UIView alloc] init]; // 自定义UIView [self addSubview:self.tableView]; [self addSubview:logout]; } return self;}@end
可以看到,SettingView继承自UIView,而不是UITableView,所以就失去了默认的行为,需要手工设置delegate,dataSource等,然后是UIViewController
@interface SettingViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>-(void) logout;@end
Controller也就不能直接继承UITableViewController,因为它的root view并不是UITableView类型,所以需要声明实现DataSource和Delegate protocol
这时候会发现,当选中table中的一行,再切换回来的时候,不会有自动取消行选中的效果,因此需要在viewDidAppear或者viewWillAppear里:
-(void) viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; SettingView *myView = (SettingView*)self.view; [myView.tableView deselectRowAtIndexPath:[myView.tableView indexPathForSelectedRow] animated:YES];}
事实上,这也正是UITableViewController的默认行为
0 0
- 自定义的UITableViewController,返回时取消row选择
- 取消树的选择状态
- 【举例】Android自定义Dialog——做出“确定/取消”的选择
- UITableViewController-简单自定义单元格
- UITableViewController 的分组
- UITableViewController的初始化问题
- 关于UITableViewController的问题
- UITableViewController的基本操作
- UITableViewController
- UITableViewController
- UITableViewController
- UITableViewController
- UItableViewController
- UITableViewController
- UITableViewController
- UITableViewController
- UITableViewController
- UITableViewController
- vi的基本命令(入门级)
- android之下载google.code
- DB2和 Oracle的并发控制(锁)比较
- 编程思想-初探
- struct和typedef struct
- 自定义的UITableViewController,返回时取消row选择
- unix网络编程进程通信 unpipc.h
- toolstripbutton 动态添加后 获取当前点击的按钮的值 并且将其删除
- Excel文件
- js 数组排除重复值(string)
- 2014年中国95家企业入围世界500强,超日本
- 宁美国度Ngame G3性能评测[图文说明]
- 云计算之IaaS
- JDK静态代理及动态代理