ios学习之UITableView(一)

对于UITableView的使用,首先必须添加<UITableViewDelegate,UITableViewDataSource>

然后再对应的地方我个人一般是在ViewDIdLoad中加入

_tableView = [[UITableView alloc] init];

_tableView.delegate = self;

_tableView.dataSource = self;


接下来就是实现UItableViewDelegate,和UITablevewDataSource中的方法啦!

首先是定义一个表单项的高度

-(CGFloat)tableView:(UITableVIew*)tableview heightForRowAtIndexPath:(NSIndexPath*)indexPath{

return //返回的是表单项的高度

}

//设置每个分区中的行数

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


//设置tableView中有多少个分区

-(NSInteger)numberOfSectionInTableView:(UItableview*)tableveiw


//设置每个CELL

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

{

static NSstring *str = @"tableViewCell";

UITableViewCell *cell =  [tableView dequeueReusableCellWithIndentifier:str];

if(!Cell)

{

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIndentifier:str];

}

cell.textLabel.text= @"hello";

return cell;

}

//当用户单击时执行的操作

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

//执行选中后的操作,如果要取消选中的效果,可以再cellForRowAtIndexPath中Cell的选中属性给位NO就可以啦!

}

//建立右边索引的数组

-(NSArray*)sectionIndextitlesForTableView:(UITableView*)tableView //the table view must be in the plain sytle(UITableViewStylePlain)


//设置每个分区的标题

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

对于这个分区标题的显示,如果你希望有不同的样式的话,可以通过代理方法tableView:viewForHeaderInSection中返回一个视图,例如UILable,当然在你调用这个

的时候你还需要注意的是这个代理的方法的实现必须和tableView:heightForHeaderInSection一起使用才行哈!


//设置分区底部标题

-(NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSinteger)section

//删除操作

-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{

NSUinteger row = [indexPath row];

if(editingStyle == UITableViewCellEditingStyleDelete)

{

[self.list removeobjectAtIndex:row];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

}

当然要在

-(UITableViewCellEditingStyle)tableView:(UItableView*)tableView edittingStyleForRowAtIndexPath:(NSIndexPath*)indexpath{

return UITabelViewCellEditingSytleDelete;

}

这样就会出现滑动出现删除键


//移动的操作

首先要执行

-(Bool)tableView:(UItableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)path

{

reutrn YES;//这样表示可以移动

}

-(void)tableView:(UItableView*)tableView moveRowAtIndexPath:(NSIndexPath*)fromIndexpath toIndexPath:(NSIndexPath*)toIndexPath

{

NSUinteger from = [fromIndexpath row];

NSUinteger to = [toIndexPath row];


id object = [self.list objectAtIndex:from];

[self.list removeAtIndex:from];

[self.list insertObject:object atIndex:to];

}


//表单插入操作

-(void)tableView:(UITableView*)tableView commitEditingStyle:(UItableViewCellEditingSytle)editingStyle forRowAtIndexPath:(NSindexPath*)indexPath

{

NSUinteger row = [indexPath row];

if(editingSytle == UITableVIewCellEditingsytleInsert){

NSarray *indexPathArray = [NSArray arrayWithObjects:indexPath];

[self.list insertObject:@"hello" atIndex:row];

[tableView insertRowsAtIndexpaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];

}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值