初入IOS之UI控件学习 UITableView

本人是大四毕业d一枚,因为实习初步涉入IOS,目前在学习IOS,所以写写blog帮助记忆。

第一个控件就玩大了,TableView,或者TableViewController。这个控件对于新手的我来说是花了一点功夫去理解的。要理解TableView是如何工作的需要了解IOS的委托代理机制。这个嘛先不说。

运行环境:黑苹果 Xcode 

1.新建一个UIViewController,在interface中添加UITableVIewDelegate和UITableVIewDataSource

2.在.m文件中添加tableview,并为对象做一些设置,添加delegate。

例子:

主要代码:

//声明并初始化对象

UITableView * tableview=[[UITableView alloc] initWithFrame:CGRectMake(0,50,320,400) style:UITableViewStylePlain];

//添加两个委托给self

tableview.delegate=self;

tableview.dataSource=self;

//在视图中添加tableview

[self.view addSubview:tableView];

//


3.添加Table在委托中必须要实现的2个必须的方法

//这是最dataSource中必须实现的方法,主要就是对cell的设置,以及重用

- (UITableVIewCell *)tableView:(UITableVIew *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

//添加重用标记

static NSString * cellid=@"cell";

//重用cell

UITableViewCell * cell=[tableView dequeueReuseableCellWithIndentifier:cellWithIndentifier];

//获得行数,以便给cell添加组件

NSUInteger row=[indexPath row];

//当cell为空时,初始化cell

if(cell==nil)

{

UITableViewCell * cell=[[UITableViewCell alloc]init];

}

//下面可以为cell添加一些title,background等等

//因为重用cell,所以当滑动到屏幕未显示的行数时必须刷新原有cell的内容。

return cell;

}

//设置一个组里面的行数,按照已有的数据来写。

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

{

return self.xxx.count;

}


以上就能显示出一个最简单的table。

下面是一些代理方法。optional但是很有用

//返回table中有多少个组

- numberOfSectionsInTableView:

//当一个cell被选中的时候除法事件

-tableView: didSelectRowAtIndexPath:

//移动一个cell

-tableView: moveRowAtIndexPath:sourceIndexPath:destinationIndexPath:

//table行是否能被修改

-tableVIew:canEditRowAtIndexPath:

// 对行进行是否可以edit进行设置

-tableView:commitEditingStyle:forRowAtIndexPath:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值