频道栏目
首页 > 资讯 > IOS > 正文

IOS开发(23)之UITableView控件

13-05-06        来源:[db:作者]  
收藏   我要投稿

1 前言
UITableVIew被分成不同部分的滚动视图,每一部分又分成不同的行,也可以创建自定义的TableVIew的行。UITableView实现了UIScrollView的垂直滚动,可以设置每行高度和行数,以及每行的内容。

2 代码实例
ZYViewController.h:


[plain]
#import <UIKit/UIKit.h> 
 
@interface ZYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>//添加代理 
 
@property(nonatomic,strong) UITableView *myTableView; 
 
@end 

#import <UIKit/UIKit.h>

@interface ZYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>//添加代理

@property(nonatomic,strong) UITableView *myTableView;

@end
ZYViewController.m:

 

[plain]
@synthesize myTableView; 
 
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.view.backgroundColor = [UIColor whiteColor]; 
    myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];//设置列表样式为简单的样式 还有一个样式为UITableViewStyleGrouped为分组模式   UITableViewStylePlain为普通的样式 
    self.myTableView.delegate = self;//设置代理为自身 
    myTableView.dataSource = self;//设置数据源为自身 
    self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//确保TablView能够正确的调整大小 
    [self.view addSubview:myTableView]; 
     

//设置每行的高度 
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    CGFloat result = 20.0f; 
    if ([tableView isEqual:self.myTableView]) { 
//        result = 40.0f; 
        result = 80.0f; 
    } 
    return result; 

//允许数据源告知必须加载到Table View中的表的Section数。 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    NSInteger result = 0; 
    if([tableView isEqual:myTableView]){ 
        result = 3;//一共三个section 
    } 
    return result; 

//设置每个Section呈现多少行 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    NSInteger result = 0; 
    if ([tableView isEqual:myTableView]) { 
        switch (section) { 
            //如果为第一个section则显示三行数据 
            case 0: 
                result=3; 
                break; 
            case 1: 
                result = 5; 
                break; 
            case 2: 
                result = 8; 
                break; 
        } 
    } 
    return result; 

//每行像是的数据 
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *result = nil; 
    if ([tableView isEqual:myTableView]) { 
        static NSString *tableViewCellIdentifier = @"MyCells";//设置Cell标识 
        result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];//通过标示符返回一个可重用的表视图单元格对象 
        if (result == nil) { 
            result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier];//初始化一个表格单元格样式和重用的标识符,并将它返回给调用者。 
        } 
        //indexPath.section 表示section的索引 indexPath.row表示行数的索引 
        result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row]; 
    } 
    return result; 

@synthesize myTableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];//设置列表样式为简单的样式 还有一个样式为UITableViewStyleGrouped为分组模式   UITableViewStylePlain为普通的样式
    self.myTableView.delegate = self;//设置代理为自身
    myTableView.dataSource = self;//设置数据源为自身
    self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//确保TablView能够正确的调整大小
    [self.view addSubview:myTableView];
   
}
//设置每行的高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat result = 20.0f;
    if ([tableView isEqual:self.myTableView]) {
//        result = 40.0f;
        result = 80.0f;
    }
    return result;
}
//允许数据源告知必须加载到Table View中的表的Section数。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger result = 0;
    if([tableView isEqual:myTableView]){
        result = 3;//一共三个section
    }
    return result;
}
//设置每个Section呈现多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger result = 0;
    if ([tableView isEqual:myTableView]) {
        switch (section) {
            //如果为第一个section则显示三行数据
            case 0:
                result=3;
                break;
            case 1:
                result = 5;
                break;
            case 2:
                result = 8;
                break;
        }
    }
    return result;
}
//每行像是的数据
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *result = nil;
    if ([tableView isEqual:myTableView]) {
        static NSString *tableViewCellIdentifier = @"MyCells";//设置Cell标识
        result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];//通过标示符返回一个可重用的表视图单元格对象
        if (result == nil) {
            result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier];//初始化一个表格单元格样式和重用的标识符,并将它返回给调用者。
        }
        //indexPath.section 表示section的索引 indexPath.row表示行数的索引
        result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];
    }
    return result;
}

 

[plain]
//点击某一行时候触发的事件 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if ([tableView isEqual:myTableView]) { 
        NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is selected",(long)indexPath.row,(long)indexPath.section]); 
    } 

//点击某一行时候触发的事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:myTableView]) {
        NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is selected",(long)indexPath.row,(long)indexPath.section]);
    }
}
运行结果:

 
 


点击某一行时候控制台显示的结果:


2013-04-28 06:46:28.021 UITableViewTest1[425:c07] Cell 4 in Section 1 is selected

2013-04-28 06:46:28.869 UITableViewTest1[425:c07] Cell 0 in Section 2 is selected

2013-04-28 06:46:31.012 UITableViewTest1[425:c07] Cell 1 in Section 2 is selected

 

相关TAG标签
上一篇:IOS开发(24)之单元格附属视图和缩进
下一篇:Windows 2008 R2集成Raid卡驱动
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站