码迷,mamicode.com
首页 > 其他好文 > 详细

UITableView代理协议总结

时间:2014-12-14 00:45:41      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:uitableview代理协议总结

UITableView代理协议总结
1./** 一共有多少组 */
-  (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
return self.personGroups.count;
}

2./** 每一组有多少行 */
- (
NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
SUNPersonGroupInfo *personGroupInfo = self.personGroups[section];
    
return personGroupInfo.persons.count;
}

3./** 显示每一行的内容 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//由于cell的复用机制,会先从自动释放池中找有没有空闲的名字相同的cell,如果有就复用,没有就创建新的,
    
static NSString*cellName=@"cell";
    
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellName];
    
if(cell==nil){
        cell=[[
UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
    }
    
//由于cell的复用机制,需要把cell的设置全部清空,不然下面的复用的时候会出错。
    cell.
textLabel.text=nil;
    cell.
detailTextLabel.text=nil;
    cell.
accessoryType=UITableViewCellAccessoryNone;
    
//分割线的设置
    cell.
selectionStyle=UITableViewCellSelectionStyleNone;
    cell.
textLabel.font=nil;
    
    
if(indexPath.section==0){
        
//设置cell的主标题和副标题(cellstyle不一样,设置的效果也不一样,有的还没有副标题)
        cell.
textLabel.text=@"主标题";
        cell.
detailTextLabel.text=@"副标题";
        
//设置cell右边附属按钮的式样,只有下面这个样式可以点击,其他样式都不能点击
        cell.
accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
        cell.
selectionStyle=UITableViewCellSelectionStyleBlue;
        cell.
textLabel.font=[UIFont systemFontOfSize:20];
        cell.
imageView.image=[UIImage imageNamed:@"001_1"];
        
//cell右边视图附属视图
        
UIButton*btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn 
addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [btn 
setTitle:@"有吗" forState:UIControlStateNormal];
        btn.
frame=CGRectMake(005030);
        cell.
accessoryView=btn;
        
//选中后的颜色又不发生改变,进行下面的设置
        
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
//不需要分割线
        
//tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    }
else{
        cell.
textLabel.text=@"第二个cell";
    }
    
return cell;
}
注意:
返回指定的row cell。这个地方是比较关键的地方,一般在这个地方来定制各种个性化的 cell元素。这里只是使用最简单最基本的cell 类型。其中有一个主标题 cell.textLabel 还有一个副标题cell.detailTextLabel,  还有一个 image在最前头 cell.imageView.  还可以设置右边的图标,通过cell.accessoryType 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头,还是勾勾标记。

4.//这个方法返回指定的 row 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

5.//这个方法返回指定的 sectionheader view 的高度。
- (CGFloat)tableView:(
UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

6.// 这个方法返回指定的 sectionfooter view 的高度。
- (CGFloat)tableView:(
UITableView *)tableView heightForFooterInSection:(NSInteger)section;

7./** 返回标题文字 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
SUNPersonGroupInfo *personGroup = self.personGroups[section];
    
return personGroup.title;
}

8./** 右侧索引 */
- (
NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    
return [self.personGroups valueForKey:@"title"];
}


// 开始编辑,一旦editing == YES就默认开启删除模式
如果 self.tableView.editing = NO;
9.// 编辑模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
return UITableViewCellEditingStyleInsert;
}
 UITableViewCellEditingStyleNone,
 UITableViewCellEditingStyleDelete,     
删除
 UITableViewCellEditingStyleInsert      
添加
10.// 可以显示拖动控件
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{

    
return YES;
}
11.//当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableview的一个属性为可以select 才行。
-(
void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
NSLog(@"%dsection中第%d行的被点击",indexPath.section,indexPath.row);
}

注意:如果不希望响应select,那么就可以用下面的代码设置属性:
TableView.allowsSelection=
NO;


12.//如何设置tableview  每行之间的 分割线 如果不需要分割线,那么就设置属性为 UITableViewCellSeparatorStyleNone  即可。
self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;

13.//如何设置 tableview cell的背景颜色
- (
UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//设置背景颜色
    cell.contentView.backgroundColor=[UIColor colorWithRed:
0.957 green:0.957 blue:0.957 alpha:1];
}

//这个函数响应,用户点击cell 右边的 箭头(如果有的话)
- (
void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

//如何设置tableview 可以被编辑,首先要进入编辑模式:如果要退出编辑模式,肯定就是设置为NO
[TableView setEditing:
YES animated:YES];

//返回当前cell  要执行的是哪种编辑,下面的代码是 返回 删除  模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath



//通知告诉用户编辑了 哪个cell,对应上面的代码,我们在这个函数里面执行删除cell的操作。
-(
void) tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

2.//滑动删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
if (editingStyle == UITableViewCellEditingStyleInsert) {  
    }
}

3.//删除的时候,左边显示的按钮,默认是delegate;
-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
return @"删除";
}

注意:默认有一个UITableView,并且self.tableview == self.view
使用以下两条语句可以跟踪验证
NSLog(
@"%p %p"self.view, self.tableView);
NSLog(
@"%@"self.view.class);

UITableView代理协议总结

标签:uitableview代理协议总结

原文地址:http://blog.csdn.net/itcontend/article/details/41919869

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!