//

//  RootViewController.m

//  LessonUITableViewBase

//

//  Created by lanouhn on 15/3/30.

//  Copyright (c) 2015年 小代码. All rights reserved.

//

 

#import "RootViewController.h"

 

@interface RootViewController()<UITableViewDataSource, UITableViewDelegate>

 

@end

 

@interface RootViewController ()

 

@end

 

@implementation RootViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColor grayColor];

    self.navigationItem.title = @"首页";

    //UITableView, 表示图, 继承于UIScrollView,

    //UITableView, 用于去展示一些列具有相同数据格式的内容

    //UITableView, 不像word的表格, 只能够展示一列内容

    

    //创建TableView, 指定大小和格式

    //格式

    //1.UITableViewStylePlain 常规表格(默认)

    //2.UITableViewStyleGrouped 组合表格

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

    //属性

    //样式, 只读属性, 只能在初始化的时候进行设定

    NSLog(@"%ld", tableView.style);

    //行高, 默认高度44

    tableView.rowHeight = 44;

    //分割线样式

    //1.UITableViewCellSeparatorStyleNone, 没有分割线

    //2.UITableViewCellSeparatorStyleSingleLine, 单行分割线(默认值)

    //3.UITableViewCellSeparatorStyleSingleLine, 单行虚线分割线

    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    //分割线颜色

    tableView.separatorColor = [UIColor redColor];

    //分割线间距, 只能够修改左右间距

    //UIEdgeInsets, 结构体, 成员变量上左下右, 多用于设置四周边距

    tableView.separatorInset = UIEdgeInsetsMake(0, 100, 0, 50);

    //背景颜色

    tableView.backgroundColor = [UIColor yellowColor];

    //背景视图

    //1.

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];

    imageView.frame = self.view.frame;

    tableView.backgroundView = imageView;

    [imageView release];

    /*

    //2.无法设置填充方式

    UIView *bigView = [[UIView alloc] initWithFrame:self.view.frame];

    bigView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"1"]];

    tableView.backgroundView = bigView;

    [bigView release];

    //3.通过图像生成颜色, 缺点, 效率不高, 不能够调整填充模式(和2一个原理)

    tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"1"]];

    //创建UIImage

    //1.通过名字找图片

    UIImage *nameImage = [UIImage imageNamed:@"1"];

    //2.通过路径找照片

    //NSBundle, 包类

    //[NSBundle mainBundle] ,找到主包, 就是找到*.app, 所有的资源都放在*.app中

    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];

    UIImage *pathImage = [UIImage imageWithContentsOfFile:path];

    */

    

    //表头

    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 375, 30)];

    headerLabel.text = @"iOS应用排行榜";

    headerLabel.textAlignment = NSTextAlignmentCenter;

    headerLabel.textColor = [UIColor redColor];

    tableView.tableHeaderView = headerLabel;

    [headerLabel release];

    //表尾

//    UILabel *footerView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 375, 30)];

//    footerView.text = @"你是猪吗";

//    footerView.textAlignment = NSTextAlignmentCenter;

//    footerView.textColor = [UIColor greenColor];

//    tableView.tableFooterView = footerView;

//    [footerView release];

    

    //消除多余的分割线

    tableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];

    

    //设置是否允许多选

    tableView.allowsMultipleSelection = YES;

    

    //tableView, 想要显示数据, 使用dataSource模式, dataSource实质是代理

    //步骤

    //1.成为tableView的数据源

    //2.实现协议中的两个必须完成的方法

    tableView.dataSource = self;

    tableView.delegate = self;

    

    [self.view addSubview:tableView];

    [tableView release];

    

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

#pragma mark - UITableViewDataSource

 

//某个分区有多少行

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

    NSLog(@"%s", __FUNCTION__);

    //只有一个分区, 或者 每个分区行数相等

    return 10;

    

    

    //多个分区, 并且行数不一致

//    if (section == 0) {

//        return 3;

//    } else {

//        return 4;

//    }

    

}

 

//显示单元格

//注: 会执行多次, 每走一次, 创建一个cell, 第一次只创建出一个屏幕能够显示的cell, 如果滚动tableView, 会在走这个方法, 再次创建cell

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

    //UITableViewCell, 单元格类, 继承UIView, 用于在UITableView上显示内容

    //样式

    //UITableViewCellStyleDefault, 默认样式, 显示imageView textLabel

    //UITableViewCellStyleValue1, 值1 显示imageView textLabel detailTextLabel

    //UITableViewCellStyleValue2, 值2 显示textLabel detailTextLabel

    //UITableViewCellStyleSubtitle, 子标题 显示imageView textLabel detailTextLabel

    /*

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

    //NSIndexPath, 在这里用于存储cell的位置(第几个分区和第一行)

    NSLog(@"分区%ld 行数%ld", indexPath.section, indexPath.row);

    cell.textLabel.text = [NSString stringWithFormat:@"分区%ld 行数%ld", indexPath.section, indexPath.row];

    cell.detailTextLabel.text = @"微软未来或将兼容Androil谷歌陷尴尬境地";

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

//    cell.contentView 是textLabel, detailTextLabel和imageView的父视图

    */

    //cell的重用机制(复用机制), 用于降低内存消耗

    

    //1.定义重用标志符

    static NSString *cellIdentifier = @"CELL";

    //2. 根据标识符, 从复用池中找cell

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    

    

    

    //3.判断是否能够找到cell

    if (cell == nil) {

        NSLog(@"嘿嘿");

        //没有找到cell, 就创建cell

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

        //辅助标示类型

        //Checkmark, 对号

        //DetailButton, 圆圈里有个'i'

        //DislosureIndicator, 大于号

        //DetailDisclosureButton = DetailButton + DislosureIndicator

        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    }

    //4.设置cell显示的内容

    cell.textLabel.text = [NSString stringWithFormat:@"分区%ld 行数%ld", indexPath.section, indexPath.row];

    

    

    

    

    return cell;

}

 

//分区个数,默认是1

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

    return 3;

}

 

//区头标题

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

    NSArray *array = @[@"A", @"B", @"C"];

    return array[section];

}

 

//区尾标题

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

    NSArray *array = @[@"AAA", @"BBB", @"CCC"];

    return array[section];

}

 

//索引(对应分区)

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    return @[@"A", @"B", @"C"];

}

 

#pragma mmark - UITableViewDelegate

 

//cell高度, 可以为每个cell指定高度

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

    if (indexPath.section == 0) {

        return 60;

    } else {

        return 30;

    }

}

 

//区头高度

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

    if (section == 1) {

        return 100;

    } else {

        return 50;

    }

}

 

//区尾高度

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

    return 60;

}

 

//自定义区头视图

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

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 44)];

    headerView.backgroundColor = [UIColor yellowColor];

    return [headerView autorelease];

}

    

//自定义区尾视图

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

    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 44)];

    footerView.backgroundColor = [UIColor redColor];

    return [footerView autorelease];

}

 

//点击某个cell

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

    NSLog(@"点击了第%ld个分区第%ld个cell", indexPath.section, indexPath.row );

}

 

//取消了

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

     NSLog(@"点击了第%ld个分区第%ld个cell", indexPath.section, indexPath.row );

}

 

 

@end

 

 

#############

小作业

 

//

//  HomeViewController.m

//  LessonUITableViewBase

//

//  Created by lanouhn on 15/3/30.

//  Copyright (c) 2015年 小代码. All rights reserved.

//

 

#import "HomeViewController.h"

#import "Book.h"

 

@interface HomeViewController () <UITableViewDataSource>

 

@property (nonatomic, retain)NSArray *bookArray;

 

@end

 

 

 

@implementation HomeViewController

 

- (void)dealloc {

    [_bookArray release];

    [super dealloc];

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    Book *book1 = [[Book alloc] initWithImageName:@"红楼梦.jpg" author:@"曹雪芹" bookName:@"红楼梦"];

    Book *book2 = [[Book alloc] initWithImageName:@"西游记.jpg" author:@"吴承恩" bookName:@"西游记"];

    Book *book3 = [[Book alloc] initWithImageName:@"三国演义.jpg" author:@"罗贯中" bookName:@"三国演义"];

    Book *book4 = [[Book alloc] initWithImageName:@"水浒传.jpg" author:@"施耐庵" bookName:@"水浒传"];

    

    self.bookArray = [NSArray arrayWithObjects:book1, book2, book3, book4,    nil];

    [book4 release];

    [book3 release];

    [book2 release];

    [book1 release];

    self.view.backgroundColor = [UIColor grayColor];

    self.navigationItem.title = @"四大名著";

    

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

    

    tableView.rowHeight = 150;

    tableView.separatorStyle = UITableViewCellStyleValue1;

    tableView.separatorColor = [UIColor redColor];

    tableView.separatorInset = UIEdgeInsetsMake(10, 50, 10, 50);

    

    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 375, 44)];

    headerLabel.text = @"四大名著";

    

    tableView.dataSource = self;

    

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];

    imageView.frame = self.view.frame;

    tableView.backgroundView = imageView;

    [imageView release];

    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    

    [self.view addSubview:tableView];

    [tableView release];

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

#pragma mark - UITableViewDataSource

 

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

    return self.bookArray.count;

}

 

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

    

    static NSString *cellIdentifier = @"CELL";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];

    }

    

    Book *tempBook = [_bookArray objectAtIndex:indexPath.row];

    cell.imageView.image = [UIImage imageNamed:tempBook.imageName];

    cell.textLabel.text = tempBook.bookName;

    cell.detailTextLabel.text = tempBook.authorname;

    return cell;

}

 

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

    return 1;

}

 

 

@end

 

posted on 2015-03-30 21:35  小雪童鞋  阅读(169)  评论(0编辑  收藏  举报