UITableView 基本使用方法总结.docx

上传人:b****6 文档编号:4086125 上传时间:2022-11-27 格式:DOCX 页数:10 大小:16.93KB
下载 相关 举报
UITableView 基本使用方法总结.docx_第1页
第1页 / 共10页
UITableView 基本使用方法总结.docx_第2页
第2页 / 共10页
UITableView 基本使用方法总结.docx_第3页
第3页 / 共10页
UITableView 基本使用方法总结.docx_第4页
第4页 / 共10页
UITableView 基本使用方法总结.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

UITableView 基本使用方法总结.docx

《UITableView 基本使用方法总结.docx》由会员分享,可在线阅读,更多相关《UITableView 基本使用方法总结.docx(10页珍藏版)》请在冰豆网上搜索。

UITableView 基本使用方法总结.docx

UITableView基本使用方法总结

1. 首先,Controller需要实现两个 delegate,分别是 UITableViewDelegate和 UITableViewDataSource

 

  2.然后UITableView对象的delegate要设置为self。

 

  3.然后就可以实现这些delegate的一些方法拉。

 

    

(1)-(NSInteger)numberOfSectionsInTableView:

(UITableView*)tableView;  

     这个方法返回tableview有多少个section 

 

     

[cpp] viewplaincopy

1.//返回有多少个Sections  

2.- (NSInteger)numberOfSectionsInTableView:

(UITableView *)tableView   

3.{  

4.    return 1;  

5.}  

 

 

     

(2)-(NSInteger)tableView:

(UITableView*)tablenumberOfRowsInSection:

(NSInteger)section;

     这个方法返回 对应的section有多少个元素,也就是多少行。

     

[cpp] viewplaincopy

1.- (NSInteger)tableView:

(UITableView *)tableView numberOfRowsInSection:

(NSInteger)section   

2.{  

3.    return 10;  

4.}  

 

 

     (3)-(CGFloat)tableView:

(UITableView*)tableViewheightForRowAtIndexPath:

(NSIndexPath*)indexPath;

          这个方法返回指定的row的高度。

         -(CGFloat)tableView:

(UITableView*)tableViewheightForHeaderInSection:

(NSInteger)section;

          这个方法返回指定的section的headerview的高度。

         -(CGFloat)tableView:

(UITableView*)tableViewheightForFooterInSection:

(NSInteger)section;

          这个方法返回指定的section的footerview的高度。

 

      (4)-(UITableViewCell*)tableView:

(UITableView*)tableViewcellForRowAtIndexPath:

(NSIndexPath*)indexPath;

         返回指定的row的cell。

这个地方是比较关键的地方,一般在这个地方来定制各种个性化的cell元素。

这里只是使用最简单最基本

         的cell类型。

其中有一个主标题cell.textLabel还有一个副标题cell.detailTextLabel, 还有一个image在最前头叫 

         cell.imageView. 还可以设置右边的图标,通过cell.accessoryType 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头,

         还是勾勾标记。

 

 

         

[cpp] viewplaincopy

1.- (UITableViewCell *)tableView:

(UITableView *)tableView cellForRowAtIndexPath:

(NSIndexPath *)indexPath   

2.{  

3.    static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";  

4.    UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:

showUserInfoCellIdentifier];  

5.    if (cell == nil)  

6.    {  

7.        // Create a cell to display an ingredient.  

8.        cell = [[[UITableViewCell alloc] initWithStyle:

UITableViewCellStyleSubtitle   

9.                                       reuseIdentifier:

showUserInfoCellIdentifier]   

10.                autorelease];  

11.    }  

12.      

13.    // Configure the cell.  

14.    cell.textLabel.text=@"签名";  

15.    cell.detailTextLabel.text = [NSString stringWithCString:

userInfo.user_signature.c_str()  encoding:

NSUTF8StringEncoding];  

16.        }  

17.          

 

 

 

       (5)-(CGFloat)tableView:

(UITableView*)tableViewheightForHeaderInSection:

(NSInteger)section

        返回指定的section的header的高度

 

         

[cpp] viewplaincopy

1.- (CGFloat)tableView:

(UITableView *)tableView heightForHeaderInSection:

(NSInteger)section  

2.{  

3.    if (section ==0)  

4.        return 80.0f;  

5.    else  

6.        return 30.0f;  

7.}  

 

 

        (6)-(NSString*)tableView:

(UITableView*)tableViewtitleForHeaderInSection:

(NSInteger)section

        返回指定的section的header 的title,如果这个sectionheader 有返回view,那么title就不起作用了。

 

         

[cpp] viewplaincopy

1.- (NSString *)tableView:

(UITableView *)tableView titleForHeaderInSection:

(NSInteger)section  

2.{  

3.    if (tableView == tableView_)  

4.    {  

5.        if (section == 0)   

6.        {  

7.            return @"title 1";  

8.        }   

9.        else if (section == 1)   

10.        {  

11.            return @"title 2";  

12.        }   

13.        else   

14.        {  

15.            return nil;  

16.        }  

17.    }   

18.    else   

19.    {  

20.        return nil;  

21.    }  

22.}  

 

 

        (7) -(UIView*)tableView:

(UITableView*)tableViewviewForHeaderInSection:

(NSInteger)section

          返回指定的sectionheader的view,如果没有,这个函数可以不返回view

         

[cpp] viewplaincopy

1.- (UIView *)tableView:

(UITableView *)tableView viewForHeaderInSection:

(NSInteger)section  

2.{  

3.    if (section == 0)   

4.    {  

5.          

6.        UIView* header = [[[NSBundle mainBundle] loadNibNamed:

 @"SettingHeaderView"   

7.                                                        owner:

 self  

8.                                                      options:

 nil] lastObject];  

9.       

10.        else  

11.        {  

12.           return nil;  

13.        }  

14.}  

 

 

        (8) -(void)tableView:

(UITableView*)tableViewdidSelectRowAtIndexPath:

(NSIndexPath*)indexPath

 

        当用户选中某个行的cell的时候,回调用这个。

但是首先,必须设置tableview的一个属性为可以select才行。

 

 

        

[cpp] viewplaincopy

1.TableView.allowsSelection=YES;  

 

 

         

[cpp] viewplaincopy

1.cell.selectionStyle=UITableViewCellSelectionStyleBlue;  

 

        如果不希望响应select,那么就可以用下面的代码设置属性:

        

[cpp] viewplaincopy

1.TableView.allowsSelection=NO;  

 

 

        下面是响应select点击函数,根据哪个section,哪个row自己做出响应就好啦。

        

[cpp] viewplaincopy

1.- (void)tableView:

(UITableView *)tableView didSelectRowAtIndexPath:

(NSIndexPath *)indexPath   

2.{  

3.    if (indexPath.section == 1)   

4.    {  

5.        return;  

6.    }  

7.    else if(indexPath.section==0)  

8.    {  

9.        switch (indexPath.row)   

10.        {  

11.            //聊天  

12.            case 0:

  

13.            {  

14.                [self  onTalkToFriendBtn];  

15.            }  

16.                break;  

17.                  

18.            default:

  

19.                break;  

20.        }  

21.    }  

22.    else   

23.    {  

24.        return ;  

25.    }  

26.      

27.}  

 

 

        如何让cell能够响应select,但是选中后的颜色又不发生改变呢,那么就设置 

        cell.selectionStyle=UITableViewCellSelectionStyleNone;

        

[cpp] viewplaincopy

1.- (UITableViewCell *)tableView:

(UITableView *)tableView cellForRowAtIndexPath:

(NSIndexPath *)indexPath  

2.{  

3.    //cell被选中后的颜色不变  

4.    cell.selectionStyle = UITableViewCellSelectionStyleNone;  

5.}  

 

 

       (9)如何设置tableview 每行之间的分割线

        

[cpp] viewplaincopy

1.self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;  

 

        如果不需要分割线,那么就设置属性为 UITableViewCellSeparatorStyleNone 即可。

 

 

       (10)如何设置tableviewcell的背景颜色

        

[cpp] viewplaincopy

1.- (UITableViewCell *)tableView:

(UITableView *)tableView cellForRowAtIndexPath:

(NSIndexPath *)indexPath  

2.{  

3.        //设置背景颜色  

4.        cell.contentView.backgroundColor=[UIColor colorWithRed:

0.957 green:

0.957 blue:

0.957 alpha:

1];  

5.}  

 

 

       (11) -(void)tableView:

(UITableView*)tableViewaccessoryButtonTappedForRowWithIndexPath:

(NSIndexPath*)indexPath

 

        这个函数响应,用户点击cell右边的箭头(如果有的话)

 

 

       (12)如何设置tableview可以被编辑

        首先要进入编辑模式:

        

[cpp] viewplaincopy

1.[TableView setEditing:

YES animated:

YES];  

 

        如果要退出编辑模式,肯定就是设置为NO

 

         -(UITableViewCellEditingStyle)tableView:

(UITableView*)tableVieweditingStyleForRowAtIndexPath:

(NSIndexPath*)indexPath

         返回当前cell 要执行的是哪种编辑,下面的代码是返回删除 模式

        

[cpp] viewplaincopy

1.- (UITableViewCellEditingStyle)tableView:

(UITableView *)tableView editingStyleForRowAtIndexPath:

(NSIndexPath *)indexPath  

2.{  

3.    return UITableViewCellEditingStyleDelete;  

4.}  

 

 

        -(void)tableView:

(UITableView*)aTableView

commitEditingStyle:

(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:

(NSIndexPath*)indexPath

        通知告诉用户编辑了哪个cell,对应上面的代码,我们在这个函数里面执行删除cell的操作。

 

 

        

[cpp] viewplaincopy

1.-(void) tableView:

(UITableView *)aTableView  

2.commitEditingStyle:

(UITableViewCellEditingStyle) editingStyle  

3.forRowAtIndexPath:

(NSIndexPath *)indexPath  

4.{  

5.        [chatArray  removeObjectAtIndex:

indexPath.row];  

6.    [chatTableView  reloadData];  

7.}  

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 初中教育 > 政史地

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1