(http://img.blog.csdn.net/20150808103801391)<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">
//
// MainViewController.m
// UI08_UITableView
//
// Created by dllo on 15/8/7.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import MainViewController.h
#import SecondViewController.h
@interface MainViewController ()
@property(nonatomic,retain)NSMutableArray *arr;
@end
@implementation MainViewController
-(void)dealloc
{
[_arr release];
[super dealloc];
}
-(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 yellowColor];
self.navigationController.navigationBar.translucent=NO;
self.navigationItem.title=@表視圖;
UITableView *tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
tableView.backgroundColor=[UIColor cyanColor];
[self.view addSubview:tableView];
[tableView release];
//設置行高
tableView.rowHeight=100;
//兩套代理的方法
tableView.dataSource=self;
//第二套協議代理人
tableView.delegate=self;
}
#pragma mark tableView第一個必須實現的協議方法,指定分區內有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//讓數組裡的元素個數和行數保持相同
// return self.arr.count;
//奇數分區有五行,偶數分區有十行
//先執行設置分區的方法,後執行每個分區有多少行
if (section % 2 == 1) {
return 5;
}else{
return 10;
}
}
#pragma mark 第二個協議方法,主要用來顯示數據
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//創建相應個數的cell
//static 特點 1.只初始化一次2.如果沒有初始值,默認是0 3.直到程序結束,才會消失
//當cell顯示結束之後,會把cell統一放到重用池中,等需要cell顯示了,先從重用池中找,看有沒有閒置cell,如果有就用閒置的cell,如果沒有在創建
//cell的重用目的是為了節約成本,用有限的cell把所有數據都顯示出來
//給重用池設置一個重用的標志,根據這個標志找到對應的重用池
//tableview通過重用標志在重用池中尋找cell,如果有閒置的cell,cell會保存一個有效地cell對象地址,如果沒有,cell裡則為nil,空
static NSString *reuse=@reuse;
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
//如果沒有cell,則進行cell的創建
if (!cell) {
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
//對cell進行賦值
//cell中有三個默認控件
cell.textLabel.text=self.arr[indexPath.row];
cell.detailTextLabel.text=[NSString stringWithFormat:@%ld,indexPath.section];
cell.imageView.image=[UIImage imageNamed:@scratch.png];
//indexPath保存的行數,從0開始,
NSLog(@%ld,indexPath.row);
return cell;
}
#pragma mark tableview裡有多少個section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
//分區的頭標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @水浒;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.arr;
}
//第二套協議
#pragma mark table的點擊方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@section:%ld,row:%ld,indexPath.section,indexPath.row);
//打印點擊的人名是什麼
NSLog(@%@,self.arr[indexPath.row]);
//點擊之後跳到下一頁
SecondViewController *secVC=[[SecondViewController alloc] init];
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// SecondViewController.m
// UI08_UITableView
//
// Created by dllo on 15/8/7.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import SecondViewController.h
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor orangeColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end