zoukankan      html  css  js  c++  java
  • UITableView表格视图、UITableView代理方法及应用

    一、基本知识点

    UITableView表格视图,是一个可以滚动的界面(理解为垂直滚动的UIScrollView),可展示多行数据,没有行数的限制,只能有一列。
    使用UITableView:
    1、展示信息,显示大量的列表数据
    2、导航
    通常选择单元格导航至另一界面。主界面是tableview,通常是导航界面(nav)的根视图,用户轻击表格中的内容,从而进入改选项的详细界面(完整信息)
     
    一、创建UITableView有两种样式
    1、 UITableView *tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];//设置样式
    2、 UITableView *tableview1 = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];//设置样式
    3、设置相关属性
    tableview.rowHeight = 80;//行高
        tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//分割线样式
        tableview.separatorColor = [UIColor greenColor];//分割线颜色
    UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
        tableview.backgroundView = imageview;//设置背景图
    //[tableview addSubview:imageview];//也可以这样添加
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 0, 20)];
        tableview.tableHeaderView = header;//tableView顶部视图,其frame只有高有效
    tableview.dataSource = self;//设置数据源
        tableview.delegate = self;
        tableview.tableFooterView = [[UIView alloc]init];//去掉线
        [self.view addSubview:tableview];
    4、代理方法
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;//设置table组数,默认是1,因此当只有一个分组时可省略
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //    if (section == 0) {
    //        return 2;
    //    }else if (section == 1) {
    //        return 6;
    //    }
        return 5;//设置每行的行数
    }
    //设置每行对应的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 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];//此处更改UITableViewCellStyleSubtitle样式,可显示不同的风格
    NSArray *arr = @[@"王",@"杨",@"韩"];
            cell.detailTextLabel.text = arr[indexPath.row];//添加字符串
     
            cell.textLabel.text = [NSString stringWithFormat:@"第%zi行,第%zi组",indexPath.row,indexPath.section];//设置cell的文本信息
            cell.imageView.image = [UIImage imageNamed:@"2.jpg"];
        }
        return cell;
    }
     
    二、UITableView代理方法及应用
    #import "ViewController.h"
    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
    @end
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        UITableView *_tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        //_tableview.rowHeight = 80;//设置行高
        //_tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [self.view addSubview:_tableview];
     
    }
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 8;
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        if (section == 0) {
            return 4;
        }else if (section == 2) {
            return 2;
        }
        return 40;
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        //static NSString *ident = @"cell";//会导致重用机制有bug
       NSString *ident = [NSString stringWithFormat:@"%zi,%zi",indexPath.section,indexPath.row];//解决重用机制bug可用这种方法//给每行设置不同的标识
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
    //    static int num;
    //    NSLog(@"%d",num++);
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ident];
            //cell.textLabel.text = nil;//先清理,再使用
        }
        cell.textLabel.text = [NSString stringWithFormat:@"%zi组,%zi行",indexPath.section,indexPath.row];
        cell.detailTextLabel.text = @"详细信息";
       
        if (indexPath.section == 0 && indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"1.jpg"];
        }
        if (indexPath.section == 2 && indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"2.jpg"];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//提示用户可点,点击之后跳至下级界面

        //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//提示用户可点,点击按钮(叹号)会有相关提示弹出,点击cell之后跳至下级界面

        //cell.accessoryType = UITableViewCellAccessoryCheckmark;//对勾
        //cell.accessoryType = UITableViewCellAccessoryDetailButton;//提示用户可点,点击按钮(叹号)会有相关提示弹出
        return cell;
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        //每组顶部视图的高度
        return 20;
    }
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        //自定义每组头视图
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40 )];
        view.backgroundColor = [UIColor redColor];
        return view;
    }
    -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
        //自定义尾部视图
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 80)];
        view.backgroundColor = [UIColor greenColor];
        return view;
    }
    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return 30;
    }

    //-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    //设置每行的高度
    //    if (indexPath.row = 0) {
    //        return 80;
    //    }
    //    return 40;
    //}
     
  • 相关阅读:
    设置eclipse编码
    前端基础知识
    微信小程序
    jQuery下拉框
    Vue-cli的安装
    vue的数据交互形式
    node安装和小测试
    shui
    JQ-滚动条下拉无限的加载数据
    HTML-video全屏
  • 原文地址:https://www.cnblogs.com/wxzboke/p/4978416.html
Copyright © 2011-2022 走看看