UITableView--基本使用

//

#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=@"设置";

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区头/尾会在上/下方显示

//Grouped区头/尾会跟着移动

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

//内存管理1+1=2 使引用计数为1

[table release];

NSLog(@"%ld",table.retainCount);

}

#pragma mark === 数据源代理方法必选

//返回区数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return3;

}

//返回行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

switch(section) {

case0:

return3;

break;

case1:

return5;

break;

default:

return6;

break;

}

}

#pragma mark === 数据源代理方法必选

//返回cell重用机制(从屏幕外进屏幕内),先去根据重用标识去重用队列中去找,没有的话要去创建(先创建后重用)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//先出列:让cell出列让可重用的cell出列dequeue队列

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

//没有就去创建创建带有重用标识的cell  return之后的代码不执行,所以要把cell放到自动释放池中返回cell方法结束之后自动释放

if(!cell) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

NSLog(@"cell是创建的");

//cell是创建的

}else{

NSLog(@"cell是重用的");

}

//cell右侧的小配件accessoryType配置,右边标识按钮DetailButton详细信息

cell.accessoryType=UITableViewCellAccessoryDetailButton;

//加载图片

cell.imageView.image= [UIImage imageNamed:imageNameArray[indexPath.row]];

//设置cell的文本标签

cell.textLabel.text=TitleAarray[indexPath.row];

return cell;

}

//TableView代理方法返回行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

//indexPath二维坐标。indexPath.section:区(没有设置的情况下,默认返回一个0区) indexPath.row:行

//indexPath索引路径

//默认44

if(indexPath.section== 0) {

return44;

}elseif(indexPath.section== 1){

return80;

}else{

return100;

}

}

//行选中的触发方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"%ldsection------%ldrow",indexPath.section,indexPath.row);

//动态取消选择灰色条选中之后消失YES的速度会慢些

[tableView deselectRowAtIndexPath:indexPath animated:YES];

UIAlertController * alert;

//防止点击一次创建一个

if(!alert) {

alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%ld区%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详细信息点击触发方法带button的type

- (void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath {

//模态页面

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];

//内存管理

[One release];

NSLog(@"accessory被点击");

}

//区头高度

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {

if(section == 0) {

return20;

}elseif(section == 1){

return40;

}else{

return60;

}

}

//区头标题

 -(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section {

if(section == 0) {

return@"0区标题";

}elseif(section == 1){

return@"1区标题";

}else{

return@"2区标题";

}

}

//区尾标题

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {

return@"区尾";

}

//区尾的高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

return40;

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容