码迷,mamicode.com
首页 > 移动开发 > 详细

【iOS开发之旅】UITableView入门示例

时间:2016-01-21 23:49:48      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:

运行效果:
技术分享                   技术分享

ViewController.h

//
//  ViewController.h
//  02-UITableView示例
//
//  Created by ChenQianPing on 16/1/21.
//  Copyright © 2016年 chenqp. All rights reserved.
//

#import <UIKit/UIKit.h>

//在ViewController头文件中,让ViewController实现两个协议
//类似Java里的实现接口
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

//需要一个数组用来初始化cell中的文字显示
@property (strong,nonatomic) NSArray *list;

@end

ViewController.m

//
//  ViewController.m
//  02-UITableView示例
//
//  Created by ChenQianPing on 16/1/21.
//  Copyright © 2016年 chenqp. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

/*
 1.为UITableView设置数据源对象
 2.让数据源对象遵守UITableViewDataSource协议
 3.在数据源对象中实现UITableViewDataSource协议的方法(一般情况下会实现3个方法)
 UITableView的数据源方法:
 1.告诉UITableView要显示几组,这个方法可以不实现,不实现默认就是分1组
    (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 2.告诉UITableView每组显示几样(几行)数据
    (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 3.告诉UITableView每一组的每一行显示什么单元格内容
 -(UITableViewCell *)tableView:(UITableView *)tableView
 cellForRowAtIndexPath:(NSIndexPath *)indexPath
 
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //初始化的动作,建立数据
    NSArray *array = [[NSArray alloc]initWithObjects:@"宝马",@"大众",@"奔弛",@"保时捷",@"兰博基尼",@"奥迪", nil];
    self.list = array;
    }

// 生成row
// 关键的步骤:实现TableView添加数据源,返回TableView的行数,返回各行cell实例

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier];
    // 这个语句根据标识符TableSampleIdentifier寻找当前可以重用的UITableViewCell。当某行滑出当前可见区域后,我们重用它所对应的UITableViewCell对象,那么就可以节省内存和资源。
    // 创建一个单元格对象并返回
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             TableSampleIdentifier];
    
    if (cell == nil) {
        // 这里UITableViewCellStyleDefault是表示UITableViewCell风格的常数,除此之外,还有其他风格,后面将会用到。
        // 设置行的风格,表示UITableViewCell风格的常量有:
        // UITableViewCellStyleDefault
        // UITableViewCellStyleSubtitle
        // UITableViewCellStyleValue1
        // UITableViewCellStyleValue2
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:TableSampleIdentifier];
    }
    
    // 注意参数(NSIndexPath *)indexPath,它将行号row和部分号section合并了,通过[indexPath row];获取行号。之后给cell设置其文本:
    // cell.textLabel.text = [self.list objectAtIndex: row];
    
    // indexPath.section 表示当前是第几组
    // indexPath.row 表示当前是第几行
    NSUInteger row = [indexPath row];
    // 为单元格指定数据
    cell.textLabel.text = [self.list objectAtIndex:row];
    //添加图片
    UIImage *image = [UIImage imageNamed:@"奔弛"];
    cell.imageView.image = image;
    //添加图片
    UIImage *highLighedImage = [UIImage imageNamed:@"奔弛"];
    cell.imageView.highlightedImage = highLighedImage;
    return cell;
  }


// 这个方法返回,对应的section有多少个元素,也就是多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.list count];
}

// 选择Table里的某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *rowString = [self.list objectAtIndex:[indexPath row]];
    NSLog(@"%@",rowString);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 UITableView显示分组数据

//
//  ViewController.m
//  03-UITableView示例-汽车标志
//
//  Created by ChenQianPing on 16/1/21.
//  Copyright © 2016年 chenqp. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//返回要显示多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //要展示3组数据
    return 3;
}

// 每一组有几条数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // 根据不同的组,返回每组显示不同条数的数据
    if (section == 0) {
        return 3;
    }else if (section == 1){
        return 2;
    }else {
        return 1;
    }
}

// 每组的第行显示什么样的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 创建单元格
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    if (indexPath.section == 0) { // 第一组 德国
        
        if (indexPath.row == 0) {
            cell.textLabel.text = @"保时捷";
            UIImage *image = [UIImage imageNamed:@"保时捷"];
            cell.imageView.image = image;
            UIImage *highLighedImage = [UIImage imageNamed:@"保时捷"];
            cell.imageView.highlightedImage = highLighedImage;

        } else if (indexPath.row == 1) {
            cell.textLabel.text = @"奔弛";
            //添加图片
            UIImage *image = [UIImage imageNamed:@"奔弛"];
            cell.imageView.image = image;
            UIImage *highLighedImage = [UIImage imageNamed:@"奔弛"];
            cell.imageView.highlightedImage = highLighedImage;
        }else{
            cell.textLabel.text = @"宝马";
            UIImage *image = [UIImage imageNamed:@"宝马"];
            cell.imageView.image = image;
            UIImage *highLighedImage = [UIImage imageNamed:@"宝马"];
            cell.imageView.highlightedImage = highLighedImage;
        }
        
        
    } else if(indexPath.section == 1){ // 第二组 意大利
        
        if (indexPath.row == 0) {
            cell.textLabel.text = @"玛莎拉蒂";
            UIImage *image = [UIImage imageNamed:@"玛莎拉蒂"];
            cell.imageView.image = image;
            UIImage *highLighedImage = [UIImage imageNamed:@"玛莎拉蒂"];
            cell.imageView.highlightedImage = highLighedImage;
        } else{
            cell.textLabel.text = @"兰博基尼";
            UIImage *image = [UIImage imageNamed:@"兰博基尼"];
            cell.imageView.image = image;
            UIImage *highLighedImage = [UIImage imageNamed:@"兰博基尼"];
            cell.imageView.highlightedImage = highLighedImage;
        }
        
    } else{ //第三组 英国
        cell.textLabel.text = @"路虎";
        UIImage *image = [UIImage imageNamed:@"路虎"];
        cell.imageView.image = image;
        UIImage *highLighedImage = [UIImage imageNamed:@"路虎"];
        cell.imageView.highlightedImage = highLighedImage;
    
    }
    return cell;
}

// 每一组的组标题显示什么
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    // 根据当前组的索引section,返回不同组的标题
    if (section == 0) {
        return @"德国";
    } else if (section == 1){
        return @"意大利";
    }else{
        return @"英国";
    }
}

// 每一组的"组尾"组描述
//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//    // 根据当前组的索引section,返回不同组的描述信息
//    if (section == 0) {
//        return @"在性能质量上却一直处于领先地位。";
//    } else if (section == 1){
//        return @"啤酒+足球+汽车,亚平宁半岛热情奔放";
//    }else{
//        return @"古典幽雅内敛不张扬过分注重内在设计和做工上的精致和思想!";
//    }
//
//}
@end

 

【iOS开发之旅】UITableView入门示例

标签:

原文地址:http://www.cnblogs.com/Bobby0322/p/5149429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!