Tableview的使用

http://archive.cnblogs.com/a/2200537/


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

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

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

数据源方法的设定

#pragma mark -

#pragma mark UITableViewDataSource methods


(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;   

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

  1. //返回有多少个Sections  
  2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView   
  3. {  
  4.     return 1;  
  5. (2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;

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

  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
  2. {  
  3.     return 10;  
  4. }  

注释:当有多段,且每段中的行数不一样的时候我们可以采用switch语句来处理

(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 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头,

                还是勾勾标记。                 

  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.         }  
其他操作的设定:

#pragma mark Table View Delegate methods


当点击tableview某行时的操作

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 35.0;

}


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

   ;

}

下面两个函数联合实现滑动删除

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 

    return YES;

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

 if (editingStyle == UITableViewCellEditingStyleDelete) { 

      1.先删除tableview中的数据

      2.再删除行

     }    

    else if (editingStyle == UITableViewCellEditingStyleInsert) { 

        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 

    }    






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值