UI学习 第六章 UITableView

UI学习          第五章                   UITableView

tableView下空cell的消除方法:table.tableFooterView = [[UIView alloc]init];
1.创建
  UITableView *tableView = [[ UITableView alloc ] initWithFrame : self . view . bounds style : UITableViewStylePlain ];
2.行高
 tableView. rowHeight = 80 ;
3.分割线样式
 tableView. separatorStyle = UITableViewCellSeparatorStyleSingleLine ;
4.分割线颜色
tableView. separatorColor = [ UIColor whiteColor ];
5.给TableView添加背景图,可以不设置背景图的frame
UIImageView *bgView = [[ UIImageView alloc ] initWithImage :[ UIImage imageNamed : @"1" ]];
tableView. backgroundView = bgView;
6.tableView顶(底)部视图,其frame只有高有效
UIView *header = [[ UIView alloc ] initWithFrame : CGRectMake ( 10 , 10 , 0 , 20 )];
    header.
backgroundColor = [ UIColor grayColor ];
   
    tableView.tableHeaderView = header;
代理方法(
tableView. dataSource = self ;
    tableView.delegate=self;
)
0.设置每行行高
-( CGFloat )tableView:( UITableView *)tableView heightForRowAtIndexPath:( NSIndexPath *)indexPath{
   
return 90 ;
}
  tableView. dataSource = self ;
    tableView.delegate=self;
1.table的组数默认是一组
-( NSInteger )numberOfSectionsInTableView:( UITableView *)tableView{
   return 1;
}
2.设置每组的行数
-( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section{
   
if (section == 0 ) {
       
return 1 ;
    }
else if (section == 1 ){
       
return 10 ;
    }
   return 4;
}
3.设置每组的cell
-( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{ //indexPath 包含了第几组: indexPath.section 和第几行 :indexPath.row
   static NSString *identifier = @"Cell";//重用机制标识
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :identifier]; // 根据重用标识,到重用池找对应的 cell
   
if (cell == nil ) {
        cell = [[
UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier :identifier]; // 创建一个 cell ,设置其样式以及其标识
    }
   // cell.backgroundColor = [UIColor clearColor];
    cell.
textLabel . text = [ NSString stringWithFormat : @" %zi , %zi " ,indexPath. row ,indexPath. section ]; // 设置 cell 的文本信息
    cell.
imageView . image = [ UIImage imageNamed : @"58" ];
   
   
return cell; // 将设置好的 cell 返回
}
cell样式
子标题式cell
cell = [[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleSubtitle reuseIdentifier :iden];
子标题在最右边式cell
[[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleValue1 reuseIdentifier :iden];
子标题紧挨标题右边式cell
[[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleValue2 reuseIdentifier :iden];

提示用户可点,点击后调到下级界面
cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator ;
提示用户可点,点击按钮(叹号)会有相关按钮弹出,点击cell会跳转到下级界面
cell. accessoryType = UITableViewCellAccessoryDetailDisclosureButton ;
显示对勾
cell. accessoryType = UITableViewCellAccessoryCheckmark ;
提示用户可点击,点击后会有相关提示弹出
cell. accessoryType = UITableViewCellAccessoryDetailButton ;
4.  每组顶部视图的高度
-( CGFloat )tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section{
    return 20 ;
}
5.  自定义每组头视图
-( UIView *)tableView:( UITableView *)tableView viewForHeaderInSection:( NSInteger )section{
   
// 自定义每组头视图
   
UIView *view = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 200 , 40 )];
    view.
backgroundColor = [ UIColor brownColor ];
   
return view;
}
6.自定义尾部视图
-( UIView *)tableView:( UITableView *)tableView viewForFooterInSection:( NSInteger )section{
    UIView *view = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 200 , 40 )];
    view.
backgroundColor = [ UIColor purpleColor ];
   return view;
}
7.设置每行的高度
-( CGFloat )tableView:( UITableView *)tableView heightForRowAtIndexPath:( NSIndexPath *)indexPath{
    if (indexPath. row == 0 ) {
       
return 80 ;
    }
   
return 40 ;
}
8.中间视图显示东西
-( NSString *)tableView:( UITableView *)tableView titleForHeaderInSection:( NSInteger )section{
   
return [ NSString stringWithFormat : @" %zi " ,section];
}
9.点击Button,cell的个数加一
-( void )addCell{
   
NSLog ( @"=====" );
    [
_array addObject : @"123" ];
    [
_tableView reloadData ]; // tableView 的数据源发生改变时,调用该方法,会更新 tableView 的显示,
}
cell选中的背景色
cell. selectionStyle = UITableViewCellSelectionStyleDefault ;
自定义cell选中背景
view. backgroundColor = [ UIColor blueColor ];
    cell.selectedBackgroundView = view;
选中状态下字体颜色
cell. textLabel . highlightedTextColor = [ UIColor whiteColor ];
自定义右侧点击视图
cell. accessoryView
10.当cell的 accessoryStyle中包含信息按钮(叹号)时,点击按钮触发的方法
  -( void )tableView:( UITableView *)tableView    accessoryButtonTappedForRowWithIndexPath:( NSIndexPath *)indexPath{    
}
11.取消选中时调用
-( void )tableView:( UITableView *)tableView didDeselectRowAtIndexPath:( NSIndexPath *)indexPath{
   NSLog(@"----%zi",indexPath.row);
}
12.cell被选中时调用
-( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath{
   NSLog(@"----%zi",indexPath.row);
   // [tableView deselectRowAtIndexPath:indexPath animated:YES];// cell 被点击时,取消选中状态
}
13.是否允许编辑
-( BOOL )tableView:( UITableView *)tableView canEditRowAtIndexPath:( NSIndexPath *)indexPath{
   
return YES ;
}
14.提交修改动作:在执行删除之前,需要先移除数组中的元素
-( void )tableView:( UITableView *)tableView commitEditingStyle:( UITableViewCellEditingStyle )editingStyle forRowAtIndexPath:( NSIndexPath *)indexPath{
   
    [
_array removeObjectAtIndex :indexPath. row ];
   
    [tableView
deleteRowsAtIndexPaths : @[ indexPath ] withRowAnimation : UITableViewRowAnimationMiddle ]; // 删除行
}
cell左侧小图标添加方法
cell. imageView . image = [ UIImage imageNamed : @"58" ];


重用机制
1、创建cell时,不从重用池找,进来就创建
NSString *identifier = [NSString stringWithFormat:@"cell"]; // 设置cell 标识

 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];  // 创建cell的同时为其附上设置的标识,进来就创建带有相同标识的cell

这种方法很耗内存,为了解决它。引用重用池,我们把之前创建好的cell放进重用池内,下面在用,直接从重用池中拿出来。拿出来后,此cell便不在重用池内。也就是说内容为“123”的cell被拿出来后,下一个显示的cell是上面又一次滑进去的空的cell。然而并不是上面每滑进去一个,下面就能拿出来用。而是当下面要‘ 创建’的这个cell 的标示符 与 上面被滑进去的cell中的某一cell的标识符一致时(按标识找),重用池中的这个cell才会被拉出去用。否则就生产一个带这个标识的cell。

2. 为了解决被找到拿出来用的cell有原内容
a、拿出来用之前,把原内容清空  cell.textLabel.text = nil;

b、给创建的每一个cell赋不同的标识
 NSString *identifier = [NSString stringWithFormat:@"cell%zi%zi",indexPath.section,indexPath.row];//给每行设置不同的标识。进入此方法,下一个要显示的cell就被赋了一个唯一标识,比如cell0组15行,与上面滑进去cell的标识都不同。它会被重新创建

c.、进入重用池找的时候,不用标示符
  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];//处理重用bug很不友好的方式,不建议使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值