IOS 学习笔记(9)tableView基础

本文详细介绍了如何使用Swift语言创建一个UITableView,并通过遵循UITableViewDelegate和UITableViewDataSource协议来自定义其外观和行为。包括如何设置单元格高度、添加数据、处理事件以及使用不同种类的附件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TableView是一个被分成不同部分的滚动视图,每一部分又进一步被分成行,每行是一个UITableViewCell类的实例。可以把图片,文本和其他任何东西嵌入tableView单元格,可以自定义他们的形状,高度,分组或更多。这些分别在UITableViewDataSource和UITableViewDelegate的协议来定义。

#import<UIKit/UIKit.h>

@interface TableViewController:UIViewController

@property(monatomic,strong)UITableView *myTableView;

@end

@implementation TableViewController

@synthesize myTableView;

-(void)viewDidLoad{

[super viewDidLoad];

self.myTableView = [ [UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

[self.view addSubview:self.myTableView];

}

这样就创建了一个空白的TableView

UITableViewStylePlain
创建一个没有背景图片的空白 Table View
UITableViewStyleGrouped
创建一个有背景图片和圆角组边框的 Table View,类似于 Settings app

怎么添加数据呢?

遵循UITableViewDelegate协议:

//设置tableviewCell高度

-(void)viewDidLoad{

[super viewDidLoad];

CGRect tableViewFrame = self.view.bounds;

self.myTableView = [[UITableView alloc]initWithFrame:tableViewFrame style:UITableViewStylePlain];

self.myTableView.delegate = self;

[self.view addSubview:self.myTableView];

}

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

CGFloat result = 20.0f;

if([tableView isEqual:self.myTableView]){

result = 40.0f;

}

return result;

}

TableView中的单元格的位置由其索引路径展示出来,一个索引路径是section和row索引的组合。section索引是从零开始的。

添加数据:

添加数据还需要遵循UITableViewDataSource协议

添加一下代码

self.myTableView.dataSource = self;

self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;


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

NSInteger result = 0;

if([tableView isEqual:self.myTableView]){

result = 3;

}

return result;

}

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

NSInteger result = 0;

if([tableView isEqual:self.myTableView]){

switch(section){

case 0:{

result = 3;

break;

}

case 1:{

result = 5;

break;

}

case 2:{

result = 8;

break;

}

}

}

return result;

}

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

UITableViewCell *result = nil;

if([tableView isEqual:self.myTableView]){

static NSString *TableViewCellIdentifier = @"MyCells";

result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];

if(result == nil){

result = [ [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier];

}

result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];

}

return result;

}


接收和处理Table view事件

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

if([tableView isEqual:self.myTableView]){

NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is Selected",(long)indexPath.row,(long)indexPath.section]);

}

}

在TableView中使用不同种类的附件

result.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

创建自定义tableView单元格附件

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

UITableViewCell *result = nil;

static NSString *myCellIdentifier = @"SimpleCell";

result = [tableView dequeueReusableCellWithIdentifier:myCellIdentifier];

if(result == nil){

result = [ [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCellIdentifier];

}

result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];

UIButton *button = [UIButton buttonWithType:UIButtonWithType:UIButtonTypeRoundedRect];

button.frame = CGRectMake(0.0f,0.0f,150.0f,25.0f);

[button setTitle:@"Expand" forState:UIControlStateNormal];

[button addTarget:self action:@selector(performExpand:) forControlEvents:UIControlEventTouchUpInside];

result.accessoryView = button;

return result;

}

-(void)performExpand:(id)paramSender{

/*Take an at ion here*/

UITableViewCell *ownerCell = (UITableViewCell *)paramSender:superview;

if(ownerCell != nil){

NSIndexPath *ownerCellIndexPath = [self.myTableView indexPathForCell:ownerCell];

NSLog(@"Accessory in index path is tapped. indexPath = %@",ownerCellIndexPath);

if(ownerCellIndexPath.section == 0 && ownerCellIndexPath.row == 1){

/*This is the second row in the first section*/

}

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值