zoukankan      html  css  js  c++  java
  • 自定的TableView

    一、自定的TableView
    有的时候,我们需要vc视图中添加一个表视图,此时在ViewController中使用TableViewController是不可行的这就,因此就要使用自定义的TableView了。
    如下图:
    1、拖拽TableView:向ViewController中添加一个TableView,如果需要自己配置自定义的cell,可以添加进去,然后为这个cell创建一个继承自TableViewCell的类,绑定它即可。
    2、设置代理:绑定ViewController为TableView的代理
    3、将拖拽的TableView成为属性
    4、实现协议中的三问
    //-------------------------------实现过程--------------------------------
    //在ViewController.m中
    #import "ViewController.h"
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
    //将TableView置为属性,这样我们就可以访问它
    @property (weak, nonatomic) IBOutlet UITableView *messageTableView;
    //存储信息
    @property(nonatomic,strong)NSMutableArray *messageArray;
    @end
    - (void)viewDidLoad {
        [super viewDidLoad];
    //注册可重用的cell,这样我们就可以在第三问就可以dequeueReusableCell,如果是自定义的cell类,也是在这注册。
        [self.messageTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    //初始化可变数组(不可变的,可以不初始化)
        self.messageArray=[NSMutableArray new];
    }
    //——————————————----------------—实现协议的方法--------------------------------
    #pragma TableView的协议
    //1、第一问:回答有几段
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    //2、第二问:回答每段有几行(Row)
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.messageArray.count;
    }
    //3、第三问:回答每行cell是什么
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell=[self.messageTableView dequeueReusableCellWithIdentifier:@"cell"];
        if(cell == nil){
            cell = [UITableViewCell alloc];
        }
        cell.textLabel.text=self.messageArray[indexPath.row];
        NSLog(@"self.messageArray[indexPath.row] = %@",self.messageArray[indexPath.row]);
        NSLog(@"cell.textLabel.text = %@",cell.textLabel.text);
        return cell;
    }
    //----------------------------End-----------------------------------
    //----------------------------纯代码创建-----------------------------------
    @interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
    @property(nonatomic,strong)UITableView *tableView;
    @end
    @implementation MainViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self setupbackgroundView];
    }
    -(void)setupbackgroundView{ 
        //创建TableView
        self.tableView=[[UITableView alloc]initWithFrame:self.view.bounds];
        self.tableView.backgroundColor=[UIColor clearColor];
    //设置代理
        self.tableView.delegate=self;
        self.tableView.dataSource=self;
        //设置分割线的颜色
        self.tableView.separatorColor=[UIColor colorWithWhite:0.1 alpha:0.3];
        //添加到view
        [self.view addSubview:self.tableView];
    }
    #pragma mark - tableview datasource
    //将第一问删除,就会默认一个section
    //第二问:
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 15;
    }
    #pragma mark - tableview delegate
    //第三问
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *ID=@"cell";
    //去队列中获取可用的cell
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
        if (!cell) {//没有的话,就创建
            cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        //设置cell的背景色
        cell.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
        switch (indexPath.section) {
            case 0:
                cell.textLabel.text=HOURS;
                break;
            case 1:
                cell.textLabel.text=DAY;
                break;
        }
        return cell;
    }
    //----------------------------End-----------------------------------
  • 相关阅读:
    jQuery---自定义动画 animate();
    jQuery---清空节点和删除节点
    HTML5与HTML4的区别
    前端开发CSS清除浮动的方法有哪些?
    关于为什么使用React新特性Hook的一些实践与浅见
    js 设计模式:观察者和发布订阅模式
    easyUI dataGrid主从表点击展开问题
    正则表达式
    JS高级---拷贝继承:把一个对象中的属性或者方法直接复制到另一个对象中
    vue-element-admin框架快速入门
  • 原文地址:https://www.cnblogs.com/lignpeng/p/5444646.html
Copyright © 2011-2022 走看看