• 2月22号 UITableView


    style:

      Grouped:有分组和分段

      Plain:没有分组和分段

    Content:

      动态单元格:表里面有多少段每段有多少行是不固定的,根据数据源来变化

      静态单元格:段和单元格是固定的不会变化(系统中的设置)


      1 @implementation ViewController
      2 
      3 - (void)viewDidLoad {
      4     [super viewDidLoad];
      5     self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStyleGrouped];
      6     _myTableView.delegate = self;
      7     _myTableView.dataSource = self;   //<UITableViewDelegate,UITableViewDataSource>两个代理都要遵守
      8     _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
      9     
     10     [self.view addSubview:_myTableView];
     11     
     12     //让表视图进入编辑状态
     13     [_myTableView setEditing:YES];
     14     
     15 }
     16 
     17 #pragma mark ---UITableDataSource---
     18 //配置有多少section  段 (默认为1)
     19 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     20     return 26;
     21 }
     22 
     23 //配置每个段有多少行
     24 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     25     return 4;
     26 }
     27 
     28 //第section段  第row行显示什么样子 (UITableViewCell)
     29 //每行显示什么样子
     30 //NSIndexPath封装了两个属性section 、row ,和一个创建NSIndexPath的类方法
     31 
     32 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     33     //重复利用机制
     34     NSString *cellID = @"cellID";
     35     
     36     //现在队列里面判断是否有可以重复利用的cell
     37     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
     38     
     39     //如果没有返回值为nil  需要自己创建一个
     40     if (cell == nil) {
     41         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
     42     }
     43     
     44     //cell显示的样式
     45     cell.textLabel.text = [NSString stringWithFormat:@"sec:%ld row:%ld", indexPath.section, indexPath.row];
     46     
     47     //返回给系统刚配置的这个cell
     48     return cell;
     49 }
     50 
     51 //当某个cell被点击后要做什么
     52 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     53     //取消点击状态
     54     //[tableView deselectRowAtIndexPath:indexPath animated:YES];
     55 }
     56 
     57 //配置cell的高度
     58 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     59     return 50;
     60 }
     61 
     62 //设置每个section头尾的标题
     63 //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
     64 //    return @"好朋友";
     65 //}
     66 //
     67 //- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
     68 //    return @"坏朋友";
     69 //}
     70 
     71 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
     72     return 50;
     73 }
     74 
     75 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
     76     return 0.000001;
     77 }
     78 
     79 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
     80     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
     81     view.backgroundColor = [UIColor greenColor];
     82     return view;
     83 }
     84 
     85 //设置cell是否可以进行编辑
     86 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
     87     if (indexPath.row == 0) {
     88         return NO;
     89     }
     90     return YES;
     91 }
     92 
     93 //更改编辑状态的按钮
     94 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
     95     //return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
     96     return UITableViewCellEditingStyleDelete;
     97 }
     98 
     99 //更改删除状态下右边显示的标题
    100 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    101     return @"Cancel";
    102 }
    103 
    104 //删除状态下右边的按钮被点击了
    105 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    106     NSLog(@"删除了");
    107 }
    108 
    109 //上下移动
    110 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    111     return YES;
    112 }
    113 
    114 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    115 }
    116 @end
  • 相关阅读:
    05mybatis配置方式简单入门
    04mybatis配置文件lombok组件使用
    03mybatis-注解方式简单入门实例
    01-02 ssm框架简介
    jsp实现文件上传
    新增的语义化标签
    html5与html4的区别
    面向对象总结
    html语法
    购物车总结
  • 原文地址:https://www.cnblogs.com/hmzxwky/p/5208114.html
Copyright © 2020-2023  润新知