iOS-UI-tabelView

来源:互联网 发布:html js 视频教程下载 编辑:程序博客网 时间:2024/05/18 01:58


//  ViewController.m

//  UI-tabelView

//

//  Created by jzq_mac on 15/7/30.

//  Copyright (c) 2015 jzq_mac. All rights reserved.

//


#import "ViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width

#define HEIGHT CGRectGetHeight(self.view.frame)

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

{

    NSArray *list;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

   

    list = @[@"..........",@"...",@"..",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""];

    

    

    

//  UITableView使用的是重用机制(滚筒原理)通过重TabelView cell 达到节省内存的目的,使用一个字符串类型的ID判断是那种cell

//  ****UITableView有两个必须实现的代理方法datasourceDelegate

// UITableView UIscrollView的子类,其所有方法属性皆可以用

    

//   UITableView有两种样式一种是平铺,一种是分组

    

    UITableView *myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, WIDTH, HEIGHT-20) style:UITableViewStylePlain];

    

    myTableView.delegate = self;

    myTableView.dataSource = self;

    

    [self.view addSubview:myTableView];

    

//    在需要更新数据的时候可以使用reloadData 来更新数据;当咱们使用reloadData的时候所有大力方法都会重新调用一次,用来更新数据

// UITableView 用的对象去调用 reloadData

// [myTableView reloadData];

    

    

}



//在这个组里面有多少行数据

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

(NSInteger)section{

    

// Section表示第几组,从0开始

    return list.count;

    

}



//显示的内容UITableViewUITableView上的每一个横格都是一个UITableViewCell


//UITableView 上面的数据 就是显示在 每一个UITableViewCell


//cellForRowAtIndexPath 有多少行就会调用多少次


//原因:他的返回值是UITableViewCell需要提供给UITableView他需要数量的cell


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

//    cellID的唯一标识

    NSString *cellID = @"cityCell";

    

// UITableView重用机制是通过查找cell的唯一标识cellID来判断这个cell对象是否存在,如果不存在,就去实例化

    

//通过cellID来查找UITableViewcell

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    

// 如果没有查找到可以重用的cell就实列化cell对象

    

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];

        

//        不要在初始化cell的地方去显示数据,否则数据会混乱

    }

    

//    显示数据的时候,写在初始化的外面

//    不能在这个方法里面去加载数据,因为这个方法会不断的调用

    

//    可以通过indexPath得到是哪一组(indexPath.section),也可以得到是哪一行(indexPath.row)都是从0开始

    cell.textLabel.text = list[indexPath.row];

    

    cell.imageView.image = [UIImage imageNamed:@"2.jpg"];

    return cell;

}



//选择cell的时候调用

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

{

    NSLog(@"%@",list[indexPath.row]);

}







- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


0 0
原创粉丝点击