UITableView是app开发中常用到的控件,功能很强大,多用于数据的显示。下面以一个简单的实例来介绍tableview的基本用法。(适合新手,高手飘过)

@interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{

UITableView *DataTable;

NSMutableArray *dataArray1; //定义数据数组1

NSMutableArray *dataArray2;//定义数据数组2

NSMutableArray *titleArray;//定义标题数组

}

- (void)viewDidLoad

{

[superviewDidLoad];

//初始化tableview

DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];//指定位置大小

[DataTablesetDelegate:self];//指定委托

[DataTablesetDataSource:self];//指定数据委托

[self.viewaddSubview:DataTable];//加载tableview

dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中国", @"美国", @"英国", nil];//初始化数据数组1

dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黄种人", @"黑种人", @"白种人", nil];//初始化数据数组2

titleArray = [[NSMutableArrayalloc] initWithObjects:@"国家", @"种族", nil];//初始化标题数组

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

//每个section显示的标题

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

switch (section) {

case 0:

return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题

case 1:

return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题

default:

return @"Unknown";

}

}

//指定有多少个分区(Section),默认为1

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

return [titleArray count];//返回标题数组中元素的个数来确定分区的个数

}

//指定每个分区中有多少行,默认为1

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

switch (section) {

case 0:

return  [dataArray1 count];//每个分区通常对应不同的数组,返回其元素个数来确定分区的行数

break;

case 1:

return  [dataArray2 count];

break;

default:

return 0;

break;

}

}

//绘制Cell

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

static NSString *CellIdentifier = @"Cell";

//初始化cell并指定其类型,也可自定义cell

UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];

  if(cell == nil)

  {

  cell = [[[UITableViewCellalloc]

  initWithFrame:CGRectZero

  reuseIdentifier:CellIdentifier] autorelease];

}

  switch (indexPath.section) {

  case 0://对应各自的分区

    [[cell textLabel]  setText:[dataArray1 objectAtIndex:indexPath.row]];//给cell添加数据

    break;

  case 1:

    [[cell textLabel]  setText:[dataArray2 objectAtIndex:indexPath.row]];

    break;

  default:

    [[cell textLabel]  setText:@"Unknown"];

}

  return cell;//返回cell

}

tableview还有很多高难度的属性和接口,在以后我会慢慢补齐。

上面的例子在功能上介绍了tableview的使用,但其在数据处理上具有很大的局限性。当我们要从服务器上请求数据,面对多种可能的数据(主要指数组的个数不稳定)此时上面的switch将无法满足我们的需求了。

使用switch可是代码的结构清晰明了,但其局限性很致命(switch中case的个数无法动态指定),下面的另一种方法可解决上述问题。

代码在原由基础上进行的修改:

@interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{

UITableView *DataTable;

NSMutableArray *dataArray1;

NSMutableArray *dataArray2;

NSMutableArray *titleArray;

NSMutableArray *dataArray; //加入了用于保存数组的数组 dataArray

}

- (void)viewDidLoad

{

[superviewDidLoad];

DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];

[DataTablesetDelegate:self];

[DataTablesetDataSource:self];

[self.viewaddSubview:DataTable];

dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中国", @"美国", @"英国", nil];

dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黄种人", @"黑种人", @"白种人", nil];

titleArray = [[NSMutableArrayalloc] initWithObjects:@"国家", @"种族", nil];

dataArray = [[NSMutableArrayalloc] initWithObjects:dataArray1, dataArray2, nil]; //初始化dataArray,元素为数组

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

//制定个性标题,这里通过UIview来设计标题,功能上丰富,变化多。

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

UIView *view = [[[UIViewalloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];

[view setBackgroundColor:[UIColorbrownColor]];//改变标题的颜色,也可用图片

UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(5, 5, 100, 30)];

label.textColor = [UIColorredColor];

label.backgroundColor = [UIColorclearColor];

label.text = [titleArrayobjectAtIndex:section];

[view addSubview:label];

return view;

}

//指定标题的高度

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

return 40;

}

//每个section显示的标题,有了上面的这个就不要了

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

}

//指定有多少个分区(Section),默认为1

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

return [titleArraycount];

}

//指定每个分区中有多少行,默认为1

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

/* switch (section) {

case 0:

return  [dataArray1 count];

break;

case 1:

return  [dataArray2 count];

break;

default:

return 0;

break;

}*/

/*  for(int i = 0; i < [titleArray count]; i++){

if(section == i){

return [[dataArray objectAtIndex:section] count];

}

}*/

  //上面的方法也是可行的,大家参考比较下

return [[dataArray objectAtIndex:section] count];  //取dataArray中的元素,并根据每个元素(数组)来判断分区中的行数。

}

//绘制Cell

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell*)[tableView

dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil)

{

cell = [[[UITableViewCellalloc]

initWithFrame:CGRectZero

reuseIdentifier:CellIdentifier] autorelease];

}

/*switch (indexPath.section) {

case 0:

[[cell textLabel]

setText:[dataArray1 objectAtIndex:indexPath.row]];

break;

case 1:

[[cell textLabel]

setText:[dataArray2 objectAtIndex:indexPath.row]];

break;

default:

[[cell textLabel]

setText:@"Unknown"];

}*/

//上面的方法也可行,大家比较下。

[[cell textLabel] setText:[[dataArrayobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row]];

//同上,取出dataArray中每个分区所对应的元素(数组),并通过其来取值。 (大家要有想像力, 复制代码试试就明白了)

return cell;

}

//改变行的高度

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

return40;

}

 
 
分类: iPhone
标签: UITableView
 

UITableView的详细使用的更多相关文章

  1. (转载)UITableView的详细讲解

    NSIndexPath类型是用来获取用户选择的indexPath,在别的函数里面,若需要知道用户选择了哪个cell,用上它可以省事很多.不必再去建全局变量section和row. NSIndexPat ...

  2. [转] 详细整理:UITableView优化技巧

      原文:http://www.cocoachina.com/ios/20150602/11968.html   最近在微博上看到一个很好的开源项目VVeboTableViewDemo,是关于如何优化 ...

  3. UITableView详细注释

    style //普通 UITableViewStylePlain, //分组 UITableViewStyleGrouped //表格视图 UITableView * tableView = [[UI ...

  4. UITableView全面解析,讲的好详细

    --UIKit之UITableView 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是U ...

  5. UITableView(二)

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  6. iOS 编辑UITableView(根据iOS编程编写)

    上个项目我们完成了 JXHomepwner 简单的应用展示,项目地址.本节我们需要在上节项目基础上,增加一些响应用户操作.包括添加,删除和移动表格. 编辑模式 UITableView 有一个名为  e ...

  7. 使用Autolayout实现UITableView的Cell动态布局和高度动态改变

    本文翻译自:stackoverflow 有人在stackoverflow上问了一个问题: 1 如何在UITableViewCell中使用Autolayout来实现Cell的内容和子视图自动计算行高,并 ...

  8. JSPatch来更新已上线的App中出现的BUG(超级详细)

    JSPatch的作用是什么呢? 简单来说:(后面有具体的操作步骤以及在操作过程中会出现的错误) 1.iOS应用程序上架到AppStore需要等待苹果公司的审核,一般审核时间需要1到2周.虽然程序在上架 ...

  9. 使用UITableView的分组样式

    分组样式顾名思义是对TableView中的数据行进行分组处理,每个分组都有一个header和footer. TableView中header的英文文本是大写的,footer的英文文本是小写的.如下图浅 ...

随机推荐

  1. python BDD&TDD

    教程一:行为驱动开发(BDD) 基于Python的行为驱动开发实战: http://python.jobbole.com/81303/ 基于Python的行为驱动开发实战 英语原文地址: http:/ ...

  2. Spring与SpringMVC的容器关系分析

    Spring和SpringMVC作为Bean管理容器和MVC层的默认框架,已被众多WEB应用采用,而实际使用时,由于有了强大的注解功能,很多基于XML的配置方式已经被替代,但是在实际项目中,同时配置S ...

  3. 414. Third Maximum Number

    这个题有点坑啊..主要是自己蠢,以为 Integer.MIN_VALUE -1 == -2147483649 public class Solution { public int thirdMax(i ...

  4. Multipath多路径冗余全解析

    一.什么是multipath 普通的电脑主机都是一个硬盘挂接到一个总线上,这里是一对一的关系.而到了有光纤组成的SAN环境,由于主机和存储通过了光纤交换机连接,这样的话,就构成了多对多的关系.也就是说 ...

  5. POI使用详解

    Apache POI使用详解 1.POI结构与常用类 (1)POI介绍 Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案 ...

  6. call, apply的用法意义以及区别是什么

    call和apply没有什么大的区别,两者的作用都是:改变对象的this指向的内容. 他们的写法不同,如下: func.call(func1, var1, var2, var3); func.appl ...

  7. ios解析XML和json数据

    解析的基本概念所谓“解析”:从事先规定好的格式串中提取数据解析的前提:提前约定好格式.数据提供方按照格式提供数据.数据获取方按照格式获取数据iOS开发常见的解析:XML解析.JSON解析 一.XML数 ...

  8. Appium测试时如何关联到Genymotion模拟器

    一.在Appium里点击左上角的Android Settings里填写模拟器的devicesName,并记得勾选和配置Application Path. (可以通过adb devices命令查询出当前 ...

  9. boost------bind的使用(Boost程序库完全开发指南)读书笔记

    bind是c++98标准库中函数适配器bind1st/bind2nd的泛化和增强,可以适配任意的可调用类型,包括函数指针.函数引用.成员函数指针和函数对象. 1.工作原理 bind并不是一个单独的类或 ...

  10. visual stduio 插件及代码生成器

    下图是本人常用的visual stuido开发工具插件. 2  使用NArrange格式化代码,这个工具,可以将代码格式化,用region分隔开来. NArrange 0.2.9.0 ________ ...