#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
iOS笔记之tableView
最新推荐文章于 2022-06-25 17:52:25 发布