UITableView学习笔记

来源:互联网 发布:python time sleep作用 编辑:程序博客网 时间:2024/06/04 17:56

  UITableView是继承自UIScrollView 当需要手动调整单元格的顺序是,就可以用tableview 移动,移动单元格到指定位置

UITableview编辑的步骤:

1让tableview处于编辑状态  -(viod)setEding:(BOOL)editing animated:(BOOL)anmated;

2指定tableview那些可以进行编辑 -(BOOL)tableview:(UITableView *)tableView canEditRowAtIndexPath:(NsIndexPath *)indexPath;

3指定tableview编辑的样式(添加 删除)-(UITableviewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(UIIndexPath *)indexPath;

4编辑完成  -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NAIndexPath *)indexPath;

tableView需要一个数据源和一个处理tableView事件的代理, 数据源需要遵循协议<UITableViewDataSource>, 代理需要遵循协议 <UITableViewDelegate>, 一般情况下tableView的代理为它自身所在的视图控制器

<UITableViewDataSource>有两个必须实现的方法:

- ( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:(NSInteger )section;

返回每个section内有多少行:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     return _tableArray.count; }

- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath;

当前屏幕范围内每个cell的内容,每显示一个cell, 数据源的代理会调用一次这个方法并从数据源中取出相应的数据给对应的cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  static NSString *cellIdentify = @"cellIdentify"; // 定义一个字符串作为重用池的标示  CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];  // 尝试从重用池中取出一个已存在的cell来使用  if (!cell) { // 如果重用池中没有就创建一个新的    cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify];  }  return cell;}

UITableViewController 继承自UIViewController  自带一个tableview,跟视图就是tableview

 UITableViewController提供一个初始化函数initWithStyle:,根据需要我们可以创建Plain或者Grouped类型的tableView,当我们使用其从UIViewController继承来的init初始化函数的时候,默认将会我们创建一个Plain类型的tableView 

  UITableViewController默认的会在viewWillAppear的时候,清空所有选中cell,我们可以通过设置self.clearsSelectionOnViewWillAppear = NO,来禁用该功能,并在viewDidAppear中调用UIScrollViewflashScrollIndicators方法让滚动条闪动一次,从而提示用户该控件是可以滑动的。


0 0