UITableView基本使用方法
1.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource
2.然后 UITableView对象的 delegate要设置为 self。
3.然后就可以实现这些delegate的一些方法拉。
(1)- (NSInteger)numberOfSectionsInTableV
这个方法返回 tableview 有多少个section
- //返回有多少个Sections
- -
(NSInteger)numberOfSectionsInTableV iew:(UITableView *)tableView - {
- return
1; - }
(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
这个方法返回
- -
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section - {
- return
10; - }
(3)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
这个方法返回指定的 row 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection
这个方法返回指定的 section的header view 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection
这个方法返回指定的 section的footer view 的高度。
- -
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - {
- static
NSString * showUserInfoCellIdentifi er = @"ShowUserInfoCell"; - UITableViewCell
* cell = [tableView_ dequeueReusableCellWithI dentifier:showUserInfoCellIdentifi er]; - if
(cell == nil) - {
- //
Create a cell to display an ingredient. - cell
= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubt itle - reuseIdentifier:showUserInfoCellIdentifi
er] - autorelease];
- }
-
- //
Configure the cell. - cell.textLabel.text=@"签名";
- cell.detailTextLabel.text
= [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding]; - }
(5)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection
返回指定的 section 的header的高度
- -
(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection :(NSInteger)section - {
- if
(section ==0) - return
80.0f; - else
- return
30.0f; - }
(6)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
返回指定的section 的 header
- -
(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section - {
- if
(tableView == tableView_) - {
- if
(section == 0) - {
- return
@"title 1"; - }
- else
if (section == 1) - {
- return
@"title 2"; - }
- else
- {
- return
nil; - }
- }
- else
- {
- return
nil; - }
- }
(7) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
返回指定的 section header 的view,如果没有,这个函数可以不返回view
- -
(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section - {
- if
(section == 0) - {
-
- UIView*
header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView" - owner:
self - options:
nil] lastObject]; -
- else
- {
- return
nil; - }
- }
(8)
当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableview的一个属性为可以select 才行。
- TableView.allowsSelection=YES;
- cell.selectionStyle=UITableViewCellSelection
StyleBlue;
如果不希望响应select,那么就可以用下面的代码设置属性:
- TableView.allowsSelection=NO;
下面是响应select 点击函数,根据哪个section,哪个row 自己做出响应就好啦。
- -
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath - {
- if
(indexPath.section == 1) - {
- return;
- }
- else
if(indexPath.section==0) - {
- switch
(indexPath.row) - {
- //聊天
- case
0: - {
- [self
onTalkToFriendBtn]; - }
- break;
-
- default:
- break;
- }
- }
- else
- {
- return
; - }
-
- }
如何让cell 能够响应 select,但是选中后的颜色又不发生改变呢,那么就设置
cell.selectionStyle = UITableViewCellSelection
- -
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - {
- //cell被选中后的颜色不变
- cell.selectionStyle
= UITableViewCellSelection StyleNone; - }
(9)如何设置tableview
- self.tableView.separatorStyle=UITableViewCellSeparator
StyleSingleLine;
如果不需要分割线,那么就设置属性为 UITableViewCellSeparator
(10)如何设置 tableview cell的背景颜色
- -
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - {
- //设置背景颜色
- cell.contentView.backgroundColor=[UIColor
colorWithRed:0.957 green:0.957 blue:0.957 alpha:1]; - }
(11) - (void)tableView:(UITableView *)tableView accessoryButtonTappedFor
这个函数响应,用户点击cell 右边的 箭头(如果有的话)
(12)如何设置tableview 可以被编辑
首先要进入编辑模式:
- [TableView
setEditing:YES animated:YES];
如果要退出编辑模式,肯定就是设置为NO
- (UITableViewCellEditingSt
返回当前cell
- -
(UITableViewCellEditingSt yle)tableView:(UITableView *)tableView editingStyleForRowAtInde xPath:(NSIndexPath *)indexPath - {
- return
UITableViewCellEditingSt yleDelete; - }
-(void) tableView:(UITableView *)aTableView
commitEditingStyle:(UITableViewCellEditingSt
forRowAtIndexPath:(NSIndexPath *)indexPath
通知告诉用户编辑了 哪个cell,对应上面的代码,我们在这个函数里面执行删除cell的操作。
- -(void)
tableView:(UITableView *)aTableView - commitEditingStyle:(UITableViewCellEditingSt
yle) editingStyle - forRowAtIndexPath:(NSIndexPath
*)indexPath - {
- [chatArray
removeObjectAtIndex:indexPath.row]; - [chatTableView
reloadData]; - }
(13)如何获得 某一行的CELL对象
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;