iOS笔记之tableView

#import "RootViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)NSMutableArray *arr;
@end

@implementation RootViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = YES;
    // 当页面只有一个tableView能滚动的视图的时候, 导航栏自动把坐标系的起点放到(0, 64)的位置, 既能保留半透明效果, 也能方便视图布局

    // 创建一个UITableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:tableView];
    [tableView release];

    // 行高
    tableView.rowHeight = 150;

    // 签订代理人, dataSource
    tableView.dataSource = self;

    // 设置代理人
    tableView.delegate = self;

}


#pragma mark  指定当前的分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}


#pragma mark  指定每一个分区的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"松江";
    } else if (section == 1) {
        return @"好汉";
    } else if (section == 2) {
        return @"金瓶梅";
    } else {
        return @"水浒传";
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 24)];
    headerView.backgroundColor = [UIColor yellowColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(300, 13, 50, 24);
    [button setTitle:@"更多" forState:UIControlStateNormal];
    [headerView addSubview:button];
    return headerView;
}


#pragma mark  tableView 的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"你瞎点什么啊!");
    NSLog(@"行--------->%ld", indexPath.row);
    NSLog(@"分区------->%ld", indexPath.section);
    NSLog(@"%@", self.arr[indexPath.row]);
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"%@", cell.textLabel.text);
}


#pragma mark tableView上的索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSArray *letter = @[@"A", @"B", @"C", @"D"];
    return letter;
}



#pragma mark 这个是dataSource实现的第一个协议方法, 返回每个分区有多少行, 分区默认是1个
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 数组元素个数跟行数进行关联
    NSLog(@"%ld", section);
    return 5;
}



#pragma mark 只要操作tableView就会触发这个协议方法, 完成对cell的创建和赋值的作用, 并且用重用的方式完成cell的创建和取用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 创建一个UITableViewCell
    // 1. 指定一个重用标志的字符串
    static int count;
    static NSString *reuse = @"reuse";
    // 2. tableView根据指定的重用标志, 在重用池里寻找有没有闲置的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    // 3. 若没有闲置的cell, 需要创建一个cell
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
        NSLog(@"我被创建了%d", count++);
    }

    // cell 创建好之后, 对cell上的属性进行复制
    cell.textLabel.text = self.arr[indexPath.row];
    //cell.textLabel.text = @"我是美女!";
    cell.detailTextLabel.text = @"你好";
    cell.imageView.image = [UIImage imageNamed:@"beauty14.jpg"];
    NSLog(@"%ld", (long)indexPath.row);
    return cell;
}

#pragma mark  返回指定位置的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 == 0) {
        return 150;
    } else {
        return 100;
    }
}


#pragma mark  返回指定分区的标题的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section % 2 == 0) {
        return 50;
    } else {
        return 20;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值