//
#import"ViewController.h"
#import"OneViewController.h"
@interface ViewController ()
{
NSArray *imageNameArray;
NSArray *TitleAarray;
}
@end
@implementation ViewController
- (void)dealloc {
//全局變量的管理釋放
[imageNameArray release];
[TitleAarray release];
//父類
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title=@"設(shè)置";
imageNameArray= [@ [@"letter_0", @"letter_1", @"letter_2", @"letter_3", @"letter_4", @"letter_5", @"letter_6", @"letter_7", @"letter_8"]retain];
TitleAarray= [@ [@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I"]retain];
//UITableView:表格
//Plain區(qū)頭/尾會在上/下方顯示
//Grouped區(qū)頭/尾會跟著移動
UITableView * table ?= [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];//1
//代理方法
table.backgroundColor= [UIColor redColor];
table.delegate=self;
table.dataSource=self;
[self.view addSubview:table];//1
//內(nèi)存管理1+1=2 使引用計數(shù)為1
[table release];
NSLog(@"%ld",table.retainCount);
}
#pragma mark === 數(shù)據(jù)源代理方法必選
//返回區(qū)數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return3;
}
//返回行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch(section) {
case0:
return3;
break;
case1:
return5;
break;
default:
return6;
break;
}
}
#pragma mark === 數(shù)據(jù)源代理方法必選
//返回cell重用機制(從屏幕外進屏幕內(nèi)),先去根據(jù)重用標識去重用隊列中去找,沒有的話要去創(chuàng)建(先創(chuàng)建后重用)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//先出列:讓cell出列讓可重用的cell出列dequeue隊列
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//沒有就去創(chuàng)建創(chuàng)建帶有重用標識的cell ?return之后的代碼不執(zhí)行,所以要把cell放到自動釋放池中返回cell方法結(jié)束之后自動釋放
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
NSLog(@"cell是創(chuàng)建的");
//cell是創(chuàng)建的
}else{
NSLog(@"cell是重用的");
}
//cell右側(cè)的小配件accessoryType配置,右邊標識按鈕DetailButton詳細信息
cell.accessoryType=UITableViewCellAccessoryDetailButton;
//加載圖片
cell.imageView.image= [UIImage imageNamed:imageNameArray[indexPath.row]];
//設(shè)置cell的文本標簽
cell.textLabel.text=TitleAarray[indexPath.row];
return cell;
}
//TableView代理方法返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//indexPath二維坐標。indexPath.section:區(qū)(沒有設(shè)置的情況下,默認返回一個0區(qū)) indexPath.row:行
//indexPath索引路徑
//默認44
if(indexPath.section== 0) {
return44;
}elseif(indexPath.section== 1){
return80;
}else{
return100;
}
}
//行選中的觸發(fā)方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%ldsection------%ldrow",indexPath.section,indexPath.row);
//動態(tài)取消選擇灰色條選中之后消失YES的速度會慢些
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIAlertController * alert;
//防止點擊一次創(chuàng)建一個
if(!alert) {
alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%ld區(qū)%ld行",indexPath.section,indexPath.row] message:TitleAarray[indexPath.row] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertActionactionWithTitle:@"確定"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*_Nonnullaction) {
NSLog(@"提示框");
}];
[alert addAction:action];
}
[self presentViewController:alert animated:YES completion:nil];
}
//accessory詳細信息點擊觸發(fā)方法帶button的type
- (void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath {
//模態(tài)頁面
OneViewController *One = [[OneViewController alloc] init];
//在one.h聲明屬性在這里接收賦值
One.section= [NSString stringWithFormat:@"%ld",indexPath.section];
One.row= [NSString stringWithFormat:@"%ld",indexPath.row];
[self presentViewController:Oneanimated:YEScompletion:nil];
//內(nèi)存管理
[One release];
NSLog(@"accessory被點擊");
}
//區(qū)頭高度
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {
if(section == 0) {
return20;
}elseif(section == 1){
return40;
}else{
return60;
}
}
//區(qū)頭標題
?-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section {
if(section == 0) {
return@"0區(qū)標題";
}elseif(section == 1){
return@"1區(qū)標題";
}else{
return@"2區(qū)標題";
}
}
//區(qū)尾標題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
return@"區(qū)尾";
}
//區(qū)尾的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return40;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end