AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
RootViewController *rootVC = [[RootViewController alloc] init];
UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = naVC;
[rootVC release];
[naVC release];
return YES;
}
RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
RootViewController.m
#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];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
// 当页面只有一个tableView能滚动的视图的时候, 导航栏会自动把坐标系的起点放到(0, 64)的位置, 既能保留半透明效果, 也方便视图的布局
self.automaticallyAdjustsScrollViewInsets = NO;
self.navigationController.navigationBar.translucent = NO;
// 创建一个UITableView
// 通过UIView四步创建
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
[self.view addSubview:tableView];
[tableView release];
tableView.backgroundColor = [UIColor yellowColor];
// 行高 default 44
tableView.rowHeight = 80;
// 签订代理人, dataSource
tableView.dataSource = self;
// 设置代理人
tableView.delegate = self;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 24)] autorelease];
headerView.backgroundColor = [UIColor redColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(300, 0, 50, 24);
[button setTitle:@"更多" forState:UIControlStateNormal];
[headerView addSubview:button];
return headerView;
}
#pragma mark 返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2 == 0) {
return 100;
} else {
return 150;
}
}
#pragma mark 设置分区标题高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 50;
}
#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: %@", cell.textLabel.text);
}
#pragma mark tableView上的索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return @[@"a", @"2", @"3"];
}
#pragma mark 指定当前的分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
#pragma mark 指定每个分区的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return @"潘金莲";
} else if(section == 1) {
return @"阎婆惜";
} else {
return @"hehe";
}
}
#pragma mark 只要操作tableView就会触发这个协议方法, 完成对cell的创建和赋值的作用, 并且用重用的方式完成cell的创建和使用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 创建UITableViewCell
// 1. 指定一个重用标志的字符串
static NSString *reuse = @"reuse";
// 2. tableView根据指定的重用标志, 在重用池里寻找有没有闲置的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
// 3. 如果没有闲置的cell, 需要创建一个cell
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
NSLog(@"我被创建了");
}
// cell创建好之后对cell上的属性进行赋值
cell.textLabel.text = self.arr[indexPath.row];
cell.detailTextLabel.text = @"222";
cell.imageView.image = [UIImage imageNamed:@"5.jpg"];
NSLog(@"%ld", indexPath.row);
return cell;
}
#pragma mark 这个是dataSource实现的第一个协议方法, 放回每个分区有多少行, 分区默认是1个
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 数组元素个数跟行数进行关联
// return self.arr.count;
if (section % 2 == 0) {
return self.arr.count;
} else {
return 10;
}
}