zoukankan      html  css  js  c++  java
  • UITableView

    UITableView是iOS中最重要的视图,没有之一。

    基本上每个应用程序都会用到UITableView来布局。

    UITableView继承于UIScrollView,所以可以滚动。可以表现为两种风格:UITableViewStylePlain和UITableViewStyleGrouped。

    UITableView基本是用来显示数据。

    UITableView的使用:

    1、在AppDelegate.m中,把UINavigationController创建出来,并给window的rootViewController。

    AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

       

        RootViewController *root = [[RootViewController alloc]init];

       

        UINavigationController *rootNC = [[UINavigationController alloc]initWithRootViewController:root];

        self.window.rootViewController = rootNC;

       

        self.window.backgroundColor = [UIColor whiteColor];

        [self.window makeKeyAndVisible];

        return YES;

    }

    2、创建RootViewController,并初始化对应的视图RootView。

    RootView.h

    #import <UIKit/UIKit.h>

     

    @interface RootView : UIView

     

    // tableView继承于Scrollview

    @property(nonatomic,retain)UITableView *tv;

    @end

    RootView.m

     

    #import "RootView.h"

     

    @implementation RootView

     

    - (instancetype)initWithFrame:(CGRect)frame

    {

        self = [super initWithFrame:frame];

        if (self) {

            [self p_setupView];

        }

        return self;

    }

     

    - (void)p_setupView{

       

        self.backgroundColor = [UIColor yellowColor];

        self.tv = [[[UITableView alloc]initWithFrame:self.bounds style:(UITableViewStyleGrouped)]autorelease];

        // 分割线的样式

        self.tv.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

        // 分割线的颜色

        self.tv.separatorColor = [UIColor greenColor];

       

        self.tv.rowHeight = 100;

    //    NSLog(@"%f",self.tv.rowHeight);

        [self addSubview:_tv];

    }

     

    - (void)dealloc

    {

        [_tv release];

        [super dealloc];

    }

     

    @end

     

    解释:

    1、self.tv = [[[UITableView alloc]initWithFrame:self.bounds style:(UITableViewStyleGrouped)]autorelease];

    第一个参数,放TableView的视图大小,位置等,一般设置跟屏幕一样大。

    第二个参数,tableview的显示样式,这里是UITableViewStyleGrouped(分组形式)。

    2、separatorStyle,tableview中,每一条信息之间,都有一条线隔开,这里设置的就是这条线的颜色。

    3、rowHeight,设置每一行的行高,如果给0,在屏幕上就不显示建好的tableview(因为每行都重叠在一起了),所以,必须要给一个值。

    @font-face { font-family: "宋体"; }@font-face { font-family: "Cambria Math"; }@font-face { font-family: "@宋体"; }@font-face { font-family: "Cambria"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Cambria; }.MsoChpDefault { font-family: Cambria; }div.WordSection1 { page: WordSection1; }

    RootViewController.m

    #import "RootViewController.h"

    #import "RootView.h"

    #import "NextViewController.h"

    // dataSource 的代理

    @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>

    @property(nonatomic,retain)RootView *rv;

    @end

     

    @implementation RootViewController

    // 有多少个分区

    // 1.确定有多少个分区

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

        return 10;

    }

     

    // 每个分区有多少行

    // 这个方法走多少次,根据有多少个分区决定

    // 2.确定每个分区有多少航

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

       

        if (section == 2) {

            return  10;

        }else{

            return 5;

        }

    }

    // 创建cell

    //这个方法走多少次,是根据各分区行数之和

    // 3.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:nil];

       

        // 重用标示符

        static NSString *cell_id = @"cell_1";

        // 先从重用池用找cell

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id ];

        // 判断是否找到cell

        if (cell == nil) {

            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cell_id];

        }

        // 给cell赋值

        cell.textLabel.text = [NSString stringWithFormat:@"section:%ld,row :%ld", indexPath.section,indexPath.row ];

    //    NSLog(@"section:%ld,row :%ld", indexPath.section,indexPath.row);

        // cell 属性

        // 文本

        cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256/255.0 green:arc4random() % 256/255.0 blue:arc4random() % 256/255.0 alpha:1];

        // 图片

        cell.imageView.image = [UIImage imageNamed:@"2.png"];

        // 子标题

        cell.detailTextLabel.text = @"测试数据..";

       

        // 辅助信息(在最右边)

    //    cell.accessoryType = UITableViewCellAccessoryCheckmark;

        cell.accessoryView = [[UISwitch alloc]init];

        // 选中样式

        cell.selectionStyle = UITableViewCellSelectionStyleBlue;

     

     return cell;

    }

    // 设置分区头标题

    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

        return [NSString stringWithFormat:@"第%ld个分区..",section];

    }

    // 设置分区尾

    -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

        return @"版权归 H.Z 所有 或者解释这个分区是干什么的。";

    }

     

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

        NSArray *arr = @[@"0",@"1",@"2",@"3",@"4"];

        return arr;

    }

    #pragma mark delegate

    // 每个分区头部view高度

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

        return 100;

    }

    // 分区头部view

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

        UISlider *s = [[UISlider alloc]initWithFrame:CGRectMake(0, 0, 200, 400)];

        s.backgroundColor = [UIColor blackColor];

        return s;

    }

    // 精准控制每一个cell

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

       

        if (indexPath.section == 0 && indexPath.row == 2) {

            return 200;

        }

        return 100;

    }

    // 选中cell

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

        NSLog(@"%@",indexPath);

       

        NextViewController *nextVC = [[NextViewController alloc]init];

        [self.navigationController pushViewController:nextVC animated:YES];

       

       

    }

    -(void)loadView{

        self.rv = [[RootView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];

        self.view = _rv;

    }

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        //设置代理

        self.rv.tv.dataSource = self;

       

        self.rv.tv.delegate = self;

    //    NSLog(@"%@",self.rv.tv);

    }

    解释:

    以下13点,都是UITableViewDataSource协议的方法。

    datasource意思是数据源,每个tableview都有数据源,负责给cell提供数据,下面13点都是dataSource协议里的方法,UITableViewDataSource跟delegate功能一样,只是名字不一样。

    1、(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;这个方法,是创建一个tableview过程中,最先执行的一个方法,这里,用来确定这个tableview有多少个分区(section)。返回值就是有多少个分区。

    2、(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section,这个方法是执行的第二个方法,意思是,这个tableview里的这个分区里,有多少行。

    3、- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath,这个方法是第三个执行的方法,也就是在这个方法里对tableview 进行初始化,创建出来。

    第一个参数:当前操作的视图,

    第二个参数:一般我们取这个对象里边的row(行)和section(分区)

    4 [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cell_id],

    创建UITableViewCell,UITableView中,每一个单元格,叫做cell,为UITableViewCell类的对象,有4种样式,UITableViewCellStyleSubtitle的样式是其中一种。

    reuseIdentifier,是“根据一个重用符号对滑出屏幕的那些cell重用”。

    在创建tableview的过程中,如果在当前屏幕中,能容下几个cell就先创建几个cell。当把tableview往上滑时,才会创建后面的cell,滑出屏幕的那些cell会被系统放入“重用池”,里边的信息没有删除,在重用池中,等待被系统重新使用。滑出屏幕的cell,已经被销毁(放进重用池),重新划回去(看到的不再是原来的cell,而是新的cell,可以是从重用池中重用的,也可能是新创建的,总之不再是原来的cell)。所以,在创建一个cell的时候,就会用一个字符串来标记,。重用机制,就是在重用池找有没有标记的cell如果有,拿出来用,如果没有,则直接创建一个新的cell。

    5、cell.textLabel.text,给cell赋值

    6、cell.imageView.image,给cell添加图片。每个cell其实有三部分组成,就是最左边的图片部分,中间的文本部分,右边的辅助信息部分。这个图片会按照cell的高按比例进行缩放,若cell行高足够高,则显示全部图片。

    7、cell.detailTextLabel.text,文本区域的子标题

    8、accessoryType,辅助信息(位于cell最右边),用于提示

    9、accessoryView,也是辅助信息的另一种形式,是对type的扩展,可以放任意视图

    10、selectionStyle,cell被选中的样式(基本失效)

    11、-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section,用来设置每一个分区的标题。

    第一个参数,当前tableview,第二个参数,当前分区

    12、-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section,设置分区尾的标题,一般用来声明版权归谁所有等等。

    13、- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView,tableview最右边的一排索引,可以点索引去到对应分区。

    UITableViewDelegate 协议的方法

    1、(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section,每一个分区头部都可以设置任意view,这个方法就是view的高度。

    2、-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section,这个方法,就是在分区头部创建一个view。这里给的view的高度,要根据上面那个方法的高度来给。

    3、- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath,这个方法是精准控制到某个区的某个cell。

    4、- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath,这个方法是,点击一个cell,能干什么事,一般用来转到下一个视图。

    5、在controller的viewDidload方法中设置代理:

    self.rv.tv.dataSource = self;dataSource的设置

    self.rv.tv.delegate = self;delegate的设置。

    一般在tableview中,这两个协议都同时遵循。

  • 相关阅读:
    【机器学习】浅谈协方差
    python {}.format
    【机器学习】准确率、精确率、召回率
    【声纹识别】 EER
    【机器学习】 最形象的入门
    逻辑卷-LVM
    RAID及软RAID的实现
    输入数字or 字符串,统计重复次数---字典统计练习
    Python-数据结构之dict(字典*****)
    POJ 3204 网络流的必须边
  • 原文地址:https://www.cnblogs.com/Coder-GT/p/4883431.html
Copyright © 2011-2022 走看看