码迷,mamicode.com
首页 > 其他好文 > 详细

UITableView的使用

时间:2015-12-18 21:23:42      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:

 

6.14.1 UITableViewDataSource协议方法初始化数据
     //设置组与组之间的间距
    self.tableView.sectionHeaderHeight=5;//footer
   

//将类与代理类建立关联,用代码或者利用连线的方式来实现

self.tableView.dataSource = self;

 
//设置tableView分组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

 //设置tableview每一组Section的行数

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

 //设置cell属性和样式
- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//添加右侧索引文字

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

   return [self.carsGroups valueForKeyPath:@"title"];

}
/tableView进入编辑状态后,每行cell编辑按钮的样式设置

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath

{

   if (indexPath.row == 0) { // 第一行是插入样式

       return UITableViewCellEditingStyleInsert;

   }else{

       return UITableViewCellEditingStyleDelete;

   }

}
//重写该方法,可实现cell滑动删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
  //先删除数据模型中的数据
   [self.contacts removeObjectAtIndex:indexPath.row];
  //再局部刷新tableVewl中的cell信息
   [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
1.1.1 }UITableViewDetegate协议方法监听操作
//tableview选中监听方法实现
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath{}
//tableview逐行设置cell行高监听方法实现
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:
6.14.2 TableView常用属性

//设置滚动区域的偏移

self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
//设置行高

self.tableView.rowHeight = 60;

//设置分割线

self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

//设置分割线颜色

self.tableView.separatorColor = [UIColor brownColor];

//cell不允许选中

self.tableView.allowsSelection = NO;

//设置分组行高

self.tableView.sectionHeaderHeight = 50;

//进入编辑状态

self.tableView.editing = YES;

 

 
/设置头部和尾部的view,同一个view来设置头部和尾部时,默认只能显示头部的

UISwitch *swithBth = [[UISwitch alloc] init];

UISwitch *swithBth1 = [[UISwitch alloc] init];

self.tableView.tableHeaderView = swithBth;

self.tableView.tableFooterView = swithBth1;

 

 TableView方法
1. 刷新
 

全部或者局部reload,都是触发tableView:cellForRowAtIndexPath:方法,创建或重用cell,局部刷新cell时还可实现动画效果。而cell.textLabel.text是直接赋值,只是更新了页面上的label属性,不触发任何事件。

reload方法执行后会清空缓存池。
2. 滚动到指定位置
 方法一:滚动到指定的单元格

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

 //方法二:可滚动到tableView底部,scrollToRowAtIndexPath只能滚动到最后一个cell,

??如果tableview包含tableFooterView则需要自己定位

[UIView animateWithDuration:0.5 animations:^{

CGFloat height = self.tableView.contentSize.height - self.tableView.frame.size.height;

[self.tableView setContentOffset:CGPointMake(0, height)];

}];

  CeLL重用

 Cell常用属性

 

/*设置backgroundView和selectedBackgroundView的背景颜色时不要直接设置cell.backgroundView.backgroundColor和cell.selectedBackgroundView.backgroundColor

应该创建ImageView后设置其backgroundColor再赋值*/

UIImageView *view = [[UIImageView alloc] init];

view.backgroundColor = [UIColor greenColor];

cell.backgroundView = view;

 

 UIImageView *view1 = [[UIImageView alloc] init];

 

view1.backgroundColor = [UIColor blueColor];

 

cell.selectedBackgroundView = view1;

 

/在设置了cell.backgroundView的背景颜色同时,也设置了cell.backgroundColor

 

//此时cell.textLabel.text = @"textLabel";

//如果在cell.backgroundColor = [UIColor redColor];后面,

 

//则label的背景颜色将会与cell.backgroundColor一致,如果在之前,
//则不会影响到label的背景颜色

 

cell.backgroundColor = [UIColor redColor];

 

cell.textLabel.text = @"textLabel";

 

//设置cell右侧按钮样式

 

cell.accessoryType = UITableViewCellAccessoryDetailButton;

 

//自定义cell右侧控件,但需要自己添加监听方法

 

cell.accessoryView = btn;

 

/设置cell的选中样式

 

self.selectionStyle = UITableViewCellSelectionStyleNone;
 1 自定义Cell与重用
 

NSString *ID = @"contact_cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

if (cell == nil) { //用户不会执行下面的语句

   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];

}

2. tableview中的静态cell被重用时,只与identifier的设置有关,与tableview 的Content无关,与cell的Style属性也无关。
3. 如果需要自定义cell,需要将style设置成Custom后,再自行拖拽控件
4. 自定义cell类时,需要自行创建一个UITableViewCell的子类,并设置cell的Class为这个子类,用这个子类来管理自定义的视图类,视图类通过Class关联了子类后,便可以进行连线管理了。
 
5 静态Cell

静态的Cell需要设置tableView的Content属性为Static Cells,然后再设置分组数量,再点击每个分组设置row

 

UITableView的使用

标签:

原文地址:http://www.cnblogs.com/linxiu-0925/p/5058157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!