iphone开发之表格组件UITableView的使用(一)使用时的具体步骤介绍

1、必须掌握的知识点
(1)设置UITableView的dataSource、delegate。
(2)UITableView多组数据和单组数据的展示。
(3)UITableViewCell的常见属性。
(4)UITableVIew的性能优化(cell的循环利用)。
(5)自定义cell。(很重要)
2、UITableView只有一列。
(1)在IOS中,要实现表格数据展示,最常用的做法就是使用UITableVIew.
(2)UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳。
3、UITableView的两种样式:
(1)UITableViewStylePlain 单条数据显示
(2)UITableViewStyleGrouped 分组数据显示
4、UITableView如何展示数据:
(1)UItableView需要一个数据源(dataSource)来显示数据。
(2)UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等。
(3)没有设置数据源的UItableView只是个空壳。
(4)凡是遵守UItableViewDataSource协议的OC对象,都可以是UITableView的数据源。
5、UITableView显示数据的具体步骤:
(1)设置数据源对象。UITableView有一个dataSource属性(指向一个数据源对象)。
(2)必须让数据源对象遵守UITableView的数据源协议,即UITableViewDataSource协议。
(3)在数据源对象中,必须实现UITableViewDataSource协议中的某些特定的方法。这些方法的作用就是告诉UITableView该如何显示数据。
(4)当UITableView运行起来的时候,会不断的调用数据源对象的这些方法来获取对应的数据,显示出来。
6、设置数据源的两种方式:
(1)代码的方式。(一般都设当前控制器为代理类对象)
需要手动为当前控制器类添加数据源协议。
self.tableView.dataSource= self;
(2)脱线的方式。

在场景中选中UITableView组件右键,单击dataSource脱线连接到控制器即可。具体如下图:


7、UITableView的数据源对象需要实现的协议方法:
(1)-(NSTnteger)numberOfSectionsInTableView:(UITableView *) tableView; 此方法是@optional类型的。
这个方法是用来告诉UITableView要显示几组。此方法返回一个数值即要分的组数。此方法也可以不实现,不实现的话就是默认分为一组。Sections是几部分,几个组的意思。
注意:此方法中的tableView参数可以根据UITableView控件设置过的不同tag值从而返回不同的分组数。
(2)-(NSInteger) tableVIew: (UITableVIew *)tableView numberOfRowsInSection:(NSInteger) section;
此方法是@required类型的,要求实现。不实现的话会发出警告。
此方法是用来告诉UITableView每组显示几条(几行)数据。即返回一个整型数值。
注意:\
-当一个View中有多个UITableView控件时,tableVIew参数可以根据已经为每个UITableView控件设置过的不同Tag值来进行区分且经常和switch...case....结构对不同的表格控件做出不同的处理。
- 当UITableView组件中有多个组时,section参数可以用来对第几组进行判断分别设置行数。例如:当前UITableView的第一组要设置3行,第二组要设置4行,第三组要设置5行。示例如下:
switch(tableView.tag)
{
case 10: // 当有多个UITableView时根据设置过的tag判断
switch(section) // 判断是第几组
{
case 0: return 3; // 第一组时返回行数是3
case 1: return 4; // 第二组时返回4行
case 2: return 5; // 第三组时返回5行
}
break;
case 20:
………
break;
default :
return 2;
break;
}
(3)-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

此方法也是@require要求实现的。主要用于告诉UITableView每组的每一行显示什么单元格内容。

indexPath仅仅封装了两个数据:
(一)section 在当前UITableView的第几组
(二)row 在当前UITableView的第几行

注意:此方法需要在内部创建一个单元格对象并返回。

代码验证如下:

新建一个具有simple View类型的IOS工程

编辑控制器的.h文件如下:

//
//  ViewController.h
//  UITableView(一)之数据显示
//
//  Created by apple on 15/8/29.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView1;
@property (nonatomic, strong) UITableView * tableView2;
@end
编辑控制器的.m文件如下:

//
//  ViewController.m
//  UITableView(一)之数据显示
//
//  Created by apple on 15/8/29.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import "ViewController.h"
#define  TABVIEWTAG1  100
#define  TABVIEWTAG2   200
#define WIDTH    [UIScreen mainScreen].bounds.size.width
#define HEIGHT  [UIScreen mainScreen].bounds.size.height
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, WIDTH, HEIGHT/2.5)];
    [self.tableView1 setBackgroundColor:[UIColor darkGrayColor]];
    self.tableView1.dataSource = self; // 设置当前控制器为数据源对象
    self.tableView1.tag  =  TABVIEWTAG1; // 设置tag值
    [self.view addSubview:self.tableView1];
    
    self.tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT-self.tableView1.frame.size.height-30, WIDTH, HEIGHT/2.5) ];
    self.tableView2.dataSource = self;
    self.tableView2.tag = TABVIEWTAG2;
    [self.view addSubview:self.tableView2];
}

- (void)didReceiveMemoryWarning
{
    
    [super didReceiveMemoryWarning];
    
}
//  此方法用来告诉UITableView要分为几组  @option类型
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    switch (tableView.tag) {
        case TABVIEWTAG1:  //为第一个UITableView组件设置为含有一个组
            return 1;
            break;
        case TABVIEWTAG2:  // 为第二个UITableView组件设置两个组
            return 2;
            break;
        default:
            return 1;
            break;
    }
}

//  此方法用来告诉哪个UITableView的哪个分组有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (tableView.tag)
    {
        case TABVIEWTAG1:
            if (section == 0) {
                return 5;
            }else
                return 10;
            break;
        case TABVIEWTAG2:
            switch (section)
        {
                case 0:
                    return 3;
                    break;
                case 1:
                    return 5;
                break;
                default:
                return 10;
                    break;
        }
            break;
        default:
            return 10;
            break;
    }
    
}

//  此方法用于告诉哪个UITableView的每一组的每一行显示什么内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell  *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    switch (tableView.tag) {
        case TABVIEWTAG1:
            cell.textLabel.text = [NSString stringWithFormat:@"我的第一个UITableView  第%d 行",indexPath.row];
            return cell;
            break;
       case TABVIEWTAG2:
            cell.textLabel.text = [NSString stringWithFormat:@"我的的第二个组件 第%d组  第%d行",indexPath.section,indexPath.row];
            return cell;
        default:
            return cell;
            break;
    }
}

@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值