对于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];
}
}