• UITableView 的用法


       UITableView是ios开发过程中极其常用的控件,主要是以列表的形式来展示数据,其杂程度相对于其他的类,可以说是最复杂的一个。

       UITableView是app开发中常用到的UI控件,功能很强大,多用于数据的显示。

    下面以一个简单的实例来介绍tableview的基本用法。

    #import "ViewController.h"
    
    
    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
    {
        UITableView *tableview;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
    
    //1、    创建,tableview有两种风格,UITableViewStylePlain和UITableViewStyleGrouped,其本质上没多大区别,主要是界面风格
    tableview=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
       // tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 400, 300) style:UITableViewStyleGrouped];
     UIImageView *image=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];//设置背景,可以是任何view或其子类的对象
        tableview.backgroundView=image;//给tableView添加背景,可以不设置背景视图的frame
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 0, 20)];
        header.backgroundColor = [UIColor grayColor];
         tableview.tableHeaderView = header;//tableview顶部视图,其frame只有高有效
      tableview.rowHeight=20;//设置行高
    tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//分割线样式
       // tableview.separatorColor = [UIColor whiteColor];//分割线的颜色
        tableview.delegate=self;//设置代理
        tableview.dataSource=self;//设置数据源
    
    [self.view addSubview:tableview];
    }
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;//table的组数,默认是一组
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        //    if (section == 0) {
        //        return 1;//第0组1行
        //    }else if (section == 1){
        //        return 10;//第一组10行
        //    }
        return 4;//设置每组的行数
    }
    //设置每行对应的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返回
    }

    运行结果如下:

  • 相关阅读:
    五子棋人机对战设计
    通过getSystemServices获取手机管理大全
    C#常见错误解决方法
    🍖数据增删改查页面搭建
    🍖django ORM 表关系与django 请求生命周期流程图
    🍖django ORM 简介
    🍖Python与django连接数据库
    🍖静态文件配置与request对象
    开启进程的两种方式
    进程PID 与PPID
  • 原文地址:https://www.cnblogs.com/wyhwyh2114/p/4971037.html
Copyright © 2020-2023  润新知