现在的位置: 首页 > 综合 > 正文

UITableView 小记

2012年05月22日 ⁄ 综合 ⁄ 共 2372字 ⁄ 字号 评论关闭

1 UITableViewController 包含了UITabelView. UITableViewController 需要实现两个  delegate ,分别是  UITableViewDelegate 和  UITableViewDataSource.

2 UITableView对象的 delegate要设置为 UITableViewController对象.

UITableViewDelegate方法介

//Sections  个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView   
{  
    return 2;  
} 


//对应的section有多少个元素,也就是多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(0 == section) return 10else return3; }

//
row 的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;//NSIndexPath 包含了section/row信息

// 返回指定的row 的cell 在这个地方来定制各种个性化的 cell元素

//主标题 cell.textLabel  副标题cell.detailTextLabel   左边图标cell.imageView.  图标cell.accessoryType  contentView cell定制试图

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;


  // 返回指定的 section 的header的高度             

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section


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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

         

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

                 

//当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableview的一个属性为可以select
// tableView.allowsSelection=YES tableView.allowsSelection=NO 拒绝选中响应事件
//cell.selectionStyle=UITableViewCellSelectionStyleBlue 选中后cell风格(UITableViewCellSelectionStyleNone, 选中不变色)

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


tableview  每行之间的 分割线 (UITableViewCellSeparatorStyleNone  不用显示分割线)

self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine       



//用户点击cell 右边的 箭头(如果有的话)

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath


 //设置tableview 可以被编辑

[tableView setEditing:YES animated:YES];  

       
//返回当前cell  要执行的是哪种编辑
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return UITableViewCellEditingStyleDelete;
}
            

//  通知告诉用户编辑了 哪个cell

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


             

如何获得 某一行的cell对象
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

文章整理自:http://blog.csdn.net/tangaowen/article/details/6438362

抱歉!评论已关闭.