1. 首先,Controller需要实现两个delegate,分别是UITableViewDelegate和UITableViewDataSource
2.然后UITableView对象的delegate要设置为self。
3.然后就可以实现这些delegate的一些方法拉。
(1)-
(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;
这个方法返回tableview有多少个section
view
plain
-
//返回有多少个Sections
-
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
-
{
-
return1;
-
}
(2)-
(NSInteger)tableView:(UITableView*)table numberOfRowsInSection:(NSInteger)section;
这个方法返回对应的section有多少个元素,也就是多少行。
view
plain
-
-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section
-
{
-
return10;
-
}
(3)-
(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
这个方法返回指定的row的高度。
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section;
这个方法返回指定的section的header
view的高度。
- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section;
这个方法返回指定的section的footer
view的高度。
(4)-
(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;
返回指定的row的cell。这个地方是比较关键的地方,一般在这个地方来定制各种个性化的cell元素。这里只是使用最简单最基本
的cell类型。其中有一个主标题cell.textLabel还有一个副标题cell.detailTextLabel,
还有一个image在最前头叫
cell.imageView. 还可以设置右边的图标,通过cell.accessoryType可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头,
还是勾勾标记。
view
plain
-
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
-
{
-
staticNSString*showUserInfoCellIdentifier=@"ShowUserInfoCell";
-
UITableViewCell*cell=[tableView_dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
-
if(cell==nil)
-
{
-
//Createacelltodisplayaningredient.
-
cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitle
-
reuseIdentifier:showUserInfoCellIdentifier]
-
autorelease];
-
}
-
-
//Configurethecell.
-
cell.textLabel.text=@"签名";
-
cell.detailTextLabel.text=[NSStringstringWithCString:userInfo.user_signature.c_str()encoding:NSUTF8StringEncoding];
-
}
-
(5)-
(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
返回指定的section的header的高度
view
plain
-
-(CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section
-
{
-
if(section==0)
-
return80.0f;
-
else
-
return30.0f;
-
}
(6)-
(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
返回指定的section的header
的title,如果这个section
header 有返回view,那么title就不起作用了。
view
plain
-
-(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section
-
{
-
if(tableView==tableView_)
-
{
-
if(section==0)
-
{
-
return@"title1";
-
}
-
elseif(section==1)
-
{
-
return@"title2";
-
}
-
else
-
{
-
returnnil;
-
}
-
}
-
else
-
{
-
returnnil;
-
}
-
}
(7)-
(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
返回指定的section header的view,如果没有,这个函数可以不返回view
view
plain
-
-(UIView*)tableView:(UITableView*)tableViewviewForHeaderInSection:(NSInteger)section
-
{
-
if(section==0)
-
{
-
-
UIView*header=[[[NSBundlemainBundle]loadNibNamed:@"SettingHeaderView"
-
owner:self
-
options:nil]lastObject];
-
-
else
-
{
-
returnnil;
-
}
-
}
(8) - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableview的一个属性为可以select才行。
view
plain
-
TableView.allowsSelection=YES;
view
plain
-
cell.selectionStyle=UITableViewCellSelectionStyleBlue;
如果不希望响应select,那么就可以用下面的代码设置属性:
view
plain
-
TableView.allowsSelection=NO;
下面是响应select点击函数,根据哪个section,哪个row自己做出响应就好啦。
view
plain
-
-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath
-
{
-
if(indexPath.section==1)
-
{
-
return;
-
}
-
elseif(indexPath.section==0)
-
{
-
switch(indexPath.row)
-
{
-
//聊天
-
case0:
-
{
-
[selfonTalkToFriendBtn];
-
}
-
break;
-
-
default:
-
break;
-
}
-
}
-
else
-
{
-
return;
-
}
-
-
}
如何让cell能够响应select,但是选中后的颜色又不发生改变呢,那么就设置
cell.selectionStyle=UITableViewCellSelectionStyleNone;
view
plain
-
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
-
{
-
//cell被选中后的颜色不变
-
cell.selectionStyle=UITableViewCellSelectionStyleNone;
-
}
(9)如何设置tableview
每行之间的分割线
view
plain
-
self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
如果不需要分割线,那么就设置属性为UITableViewCellSeparatorStyleNone 即可。
(10)如何设置tableview
cell的背景颜色
view
plain
-
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
-
{
-
//设置背景颜色
-
cell.contentView.backgroundColor=[UIColorcolorWithRed:0.957green:0.957blue:0.957alpha:1];
-
}
(11)-
(void)tableView:(UITableView*)tableViewaccessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath
这个函数响应,用户点击cell右边的箭头(如果有的话)
(12)如何设置tableview可以被编辑
首先要进入编辑模式:
view
plain
-
[TableViewsetEditing:YESanimated:YES];
如果要退出编辑模式,肯定就是设置为NO
- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath
返回当前cell 要执行的是哪种编辑,下面的代码是返回删除模式
view
plain
-
-(UITableViewCellEditingStyle)tableView:(UITableView*)tableVieweditingStyleForRowAtIndexPath:(NSIndexPath*)indexPath
-
{
-
returnUITableViewCellEditingStyleDelete;
-
}
-(void) tableView:(UITableView*)aTableView
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
forRowAtIndexPath:(NSIndexPath*)indexPath
通知告诉用户编辑了哪个cell,对应上面的代码,我们在这个函数里面执行删除cell的操作。
view
plain
-
-(void)tableView:(UITableView*)aTableView
-
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
-
forRowAtIndexPath:(NSIndexPath*)indexPath
-
{
-
[chatArrayremoveObjectAtIndex:indexPath.row];
-
[chatTableViewreloadData];
-
}
(13)如何获得某一行的CELL对象
- (UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath;
相关推荐
UITableView是iPhone中比较常用的,用的比较多的控件, 本例中说明iPhone UITableView的使用方法实例。 该实列中是手动增加UITableViewDataSource和UITableViewDelegate协议来实现的。
UITableView、UITableView基本用法、UITableView详解,UITabelView常用方法
UITableView的使用方法及cell复用机制
UITableView基本用法大全
在iOS开发中会用到的UISCrollView与UITableView嵌套使用的方法
UITableView的使用,UITableView所在的UIViewController声明两个delegate:UITableViewDelegate和UITableViewDataSource。
UITableView的使用 包括简单的搭建UITableView、搭建分组的UITableView、搭建分段的UITableView、可删除行等等
iOS基本控件UITableView示例,展示了基本tableview的方法。
UITableView的简单使用
uitableview基本方法使用,代理方法、编辑、移动等。
UITableView高度自适应解决方法 iOS 源码 下载
UI 使用总结之--UITableView
UITableView相关操作
iOS开发中UITableview控件的基本使用及性能优化方法共15页.pdf.zip
iOS使用UITableView实现的富文本编辑器iOS使用UITableView实现的富文本编辑器iOS使用UITableView实现的富文本编辑器iOS使用UITableView实现的富文本编辑器iOS使用UITableView实现的富文本编辑器iOS使用UITableView...
代码仅供参考,有问题可以 联系我
UITableView Demo 快捷菜单 粘贴 Cell
IOS代码中使用自定义UITableView,包含了一些开发过程中需要注意的地方
UITableView通过重用单元格来达到节省内存的目的:通过为每个单元格指定一个重用标识符 reuseIdentifier 即指定了单元格的种类 以及当单元格滚出屏幕时 允许恢复单元格以便重用 对于不同种类的单元格使用不同的ID ...
iphone UITableView中使用combobox的实例,实现简单方便,但蛮好用的。