• IOS 学习:UITableView 使用详解1


    IOS 学习:UITableView 使用详解1

    tableView作为最常见的视图之一在ios应用之中占有举足轻重的地位,因此学习tableView是非常重要的。这篇文章粗略的介绍tableView的创建过程和几个基本的委托方法。

    1.添加委托和成员变量。

    在视图控制器类的头文件当中,声明UITableView 指针变量table,数组datalList作为数据源。添加UITableViewDelegate 和UITableViewDataSource委托,具体添加方式如下。

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

    @property(nonatomic,retain)NSArray *dataList;

    @property(nonatomic,retain)UITableView *table;

    @end

    2.创建UITableView和其他相关对象。

    在viewDidLoad方法下添加创建表格试图对象和进行数据源的初始化。

    dataList=[[NSArrayalloc]initWithObjects:

                  @"武汉",

                  @"重庆",

                  @"上海",

                  @"北京",

                  @"深圳",

                  @"广州",

                  @"重庆",

                  @"香港",

                  @"台海",

                  @"天津",nil];

        table=[[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];

        table.backgroundView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"iphone.jpg"]];

        table.delegate=self;

        table.dataSource=self;

        [self.viewaddSubview:table];

    这里重点说一下UITableViewStyle,IOS提供了两种表格样式,即普通样式UITableViewStylePlain和分组样式UITableViewStyleGrouped。

    两种样式分别如图:

    将表格的delegate和dataSource设置为self,这样我们自定义的委托方法才会被调用。

    3.添加委托方法

    数据原方法:UITableViewDataSource

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

    {

        return [dataListcount];

    }

    这个方法返回表格每个分组的行数,非分组的表格默认为一个分组。

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {

        return1;

    }

    这个方法返回表格的组数

    表格视图委托方法:UITableViewDelegate

    这里的方法都是可选择的,因此可以根据需求使用

    这里我只使用了4个方法,效果如上图。

    1.创建单元格。

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

    {

        NSString *identifier=@"identifier";

        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];

        if(cell==NULL)

        {

            cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];

        }

        NSInteger row=[indexPath row];

        cell.textLabel.text=[dataListobjectAtIndex:row];

        return cell;

    }

    这个函数是具体创建每个单元格所使用的方法, dequeueReusableCellWithIdentifier 方法是检查队列中是否有可重用的单元格,如果没有则进入if条件语句新建单元格。否则跳过新建过程,这样有利于提高表格滚动时的运行速度。

    接下来,给每个单元格的标签赋值cell.textLabel.text=[dataList objectAtIndex:row];

    然后返回该单元格。

    2.呈现单元格之前的设置动作。

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

    {

        if([indexPath row]%2==0)

        {

            cell.backgroundColor=[UIColorgreenColor];

        }

        else{

            cell.backgroundColor=[UIColorblueColor];

        }

    }

    当一个单元格将被呈现是,触发此方法,这个方法内部完成个每个单元格的背景颜色设置工作。

    当为偶数行市,单元格函数是绿色,否则将是蓝色。

    3.表格选择函数。

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

    {

        UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"提示"message:dataList[indexPath.row]delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil, nil];

        [tableView deselectRowAtIndexPath:indexPath animated:YES];

        [alert show];

    }

    当用户选择某个方法是,将触发此方法。在这个方法内部,新建了一个警告对话框,当选择某个单元格市,将弹出对话框。

    deselectRowAtIndexPath方法就是取消选择状态,选择某个单元格后,该单元格变成了蓝色,调用该方法后就会返回未被选择时候所呈现的颜色。

    4.滑动删除

    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    {

        NSLog(@"执行删除");

    }

    当用户在某个单元格上滑动式时,会出现一个Delete按钮,用户点击该按钮,触发此方法。

     

  • 相关阅读:
    读Pyqt4教程,带你入门Pyqt4 _009
    [Objective-C] 006_Protocol(协议)
    [JavaWeb基础] 003.JAVA访问Mysql数据库
    keras学习笔记-bili莫烦
    CentOS7安装codeblocks
    [转] Linux多线程编程之pthread
    [转] C++ 的关键字(保留字)完整介绍
    如何生成红黑树?一步一步画给你看(从二叉查找树到2-3树再到红黑树)
    C++标准库(体系结构与内核分析)(侯捷第二讲)(OOP GP、分配器、容器之间关系、list、萃取机、vector、array、deque、stack、queue、RB-tree、set、multiset、map、multimap)
    C++标准库(体系结构与内核分析)(侯捷第一讲)(标准库介绍、STL、六大部件、算法、容器分类、Array、Vector、List、Forward-List、Deque、stack、queue、multiset、multimap、分配器)
  • 原文地址:https://www.cnblogs.com/jackwuyongxing/p/3516639.html
Copyright © 2020-2023  润新知