您的位置:首页 > 移动开发 > IOS开发

iOS开发Tableview配置单元格相关内容

2016-06-24 11:18 323 查看
1.表 

//UITabelView : UIScrollVIew

创建表的样式有 plain (平铺)  grouped (分组)

调用协议方法时  需要设置数据源   dataSource 
和 代理 delegate 
并在.h文件中导入

必须实现的两个协议方法

1. 每一区返回多少行  返回值类型 : NSInteger

-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section;

2.配置单元格 返回值类型 : UITableViewCell


//UITableViewCell : UIView 

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

配置单元格时 :

1.系统将单元格所在的区号 section 行号  row 包装成indexPath 类型的对象给我们 
并且他们的索引都是从 0 开始

2. 单元格的重用

//为什么要单元格重用?因为表的使用以及表上控件的加载或者图片等数据的处理时,还是很耗费资源的。所以假如说,当我们滑动单元格的时候,如果一直新建并配置新的单元格的话,肯定会对程序的运行造成负荷,因此单元格的重用在表的使用中尤为重要!

//单元格重用步骤:
1).  创建静态的重用标示符

static NSString *
cellID = @"cell";

2). 在重用队列中找到打着cellID
标记的cell 

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];

   UITableViewCell *cell
=[tableView cellForRowAtIndexPath:indexPath];

3). 如果队列中找不到  则重建cell

if (!cell) {

            //创建cell  指定样式  并且  设置重用标示符  打上标记

        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellID]autorelease];

}

4).在判断外面返回cell

    单元格的三大属性:
cell .imageView .image =
[UIImage  imageNamed:@“…”];
cell .textLabel .text = [NSString stringWithFormat:@"section
= %d   row = %d",indexPath.section,indexPath
.row];

//单元格详细内容

  cell .detailTextLabel .text = @"QQ"; 

//附加小挂件

cell .accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

//单元格个别协议方法,更多方法可以参考系统文档。

3. 设置区头  区尾 标题
返回值类型  NSString *

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

{

     return @“……”;

}

-(NSString *)tableView:(UITableView *)tableView
titleForFooterInSection:(NSInteger)section;

4. 设置tableView  的行高 
如果所有行高都一样的话  就通过 rowHight s属性设置 
如果不一样  调用下面的协议方法 

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

5.选中单元格的时候,需要做的事情

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

 //取消选中某一行单元格

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

6. 点击设置的小挂件的时候  调用的方法

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

************************自定义单元格方法*************************

2.自定义单元格 

1. 自定义单元格的时候  因为cell 也是view  所以可以在上边添加子控件 
控件的添加可以写在if 语句中 相同属性的设置写在if语句中  不同属性的设置  写在if语句外

//注意 :  子控件是添加在cell 上的

[cell addSubview:子控件];

在if 语句外设置子控件的属性时  可以通过设置tag值  找到在if语句中创建的 子控件

 
UILabel * leftLabel = (UILabel *)[cell viewWithTag:tag];

2.如果在cell上添加的子控件 中有button  需要在button绑定的方法中找到我们点击的是哪个button的时候 
进行以下步骤:

1).首先找到button所在的cell  (通过button 调用 superview 找到 cell) 

//不同版本的Xcode 以及我们自定义cell的方法不同  我们通过button 找 cell 调用 superview 的次数也不相同 
所以 可以先输出一下 
看我们需要调用几次

 NSLog(@"---------------------------------%@",button
.superview.superview);

  UITableViewCell *
cell = (UITableViewCell *)button.superview.superview;

2).根据cell 找到这个单元格所对应的 indexPath 

       UITableView * tableView = (UITableView *)[self .view viewWithTag:111];(通过设置全局变量或者tag值
先找到tableView)

   

       NSIndexPath * indexPath =    [tableView indexPathForCell:cell];

自定义单元格 (2) 

1.添加子类  继承于  UITableViewCell  在子类上添加子控件

在子类的属性描述中 
要注意 不要和系统的三大属性 重名 

 并将创建的子控件赋值给一个属性 即:

self .myLabel =
label;

如过在子类创建的子控件中有按钮  那么按钮绑定方法  需要回到原类中绑定和实现

自定义单元格 (3)

通过xib 文件进行拖拽 

1. 拖拽的子控件需要在.h 文件中绑定方法

//xib 文件  加载cell 对象

//根据xib 文件名字  绘制一条路径出来 并且在该方法的内部 将xib 文件中试图对象加载出来  放在数组中

NSArray * array =     [[NSBundle mainBundle]loadNibNamed:@"ZYTableViewCell" owner:nil options:nil];

1).         cell =
[array firstObject];

              则此时 可以对cell 上的子控件进行属性设置 即 :

               cell .myLabel .text
= ……

2).           //遍历数组
              for (UIView *
view  in array) {

           

                if ([view isMemberOfClass:[ZYTableViewCell class]])
{

               

                cell =(ZYTableViewCell *)
view;

               

                NSLog(@"view===%@",view);

                break;

                      }

                }
                则此时 可以对cell 上的子控件进行属性设置 即 :

               cell .myLabel .text
= ……

//注意 :  自定义cell  cell上的button 如果是通过 xib 拖拽的  需要调用三次superview 才能找到 cell

补充 :

//注意: 

    // (对象)isMemberOfClass  : 自身类才能进

   

    // (对象)isKindOfClass : 自身类 或者父类  条件都成立

    UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];

   

    if ([btn isMemberOfClass:[UIButton class]])
{

       

        NSLog(@"11111111111111");

       

    }

2. 通过xib 文件中的使用工具区 无法完成的一些初始化的设置  我们可以通过代码在子类的.m 文件中的  - (void)awakeFromNib  ; 
方法中实现 例:

//设置圆角

    _myImageView .layer .cornerRadius=
40;

   

    //_myImageView .clipsToBounds = YES;

   

    _myImageView .layer .masksToBounds = YES;

自定义单元格 (4)

1.当自定义单元格里有两种样式的单元格时   需要声明两个静态标示符  并分别判断

 static NSString *
cellID1 = @"first";

    static NSString *
cellID2 = @"two";

   

    if (indexPath.section%2
== 0) {//展示第一种样式的单元格

   

        firstTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID1];

       

        if (!cell)

    {

           

            cell = [[[NSBundle mainBundle]loadNibNamed:@"firstTableViewCell" owner:nil options:nil]lastObject];

    }

        return cell;

       

    }

    else

    {//展示第二种单元格

        twoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID2];

       

        if (!cell)

    {

           

            cell = [[[NSBundle mainBundle]loadNibNamed:@"twoTableViewCell" owner:nil options:nil]lastObject];

           

         

    }

        return cell;

    }

2.设置区头 区尾 的view  需要调用一下协议方法

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

区头视图的重用  类似与cell的重用

  static NSString *
viewID = @"view";

  UITableViewHeaderFooterView * view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:viewID];

   

    if (!view) {

       

        view = [[[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:viewID]autorelease];

//设置contentView 的背景颜色

        view .contentView .backgroundColor =
[UIColor grayColor];

//设置区头view 的图片

UIImageView * img = [[UIImageView alloc]initWithFrame:CGRectMake(0,
0, 320, 50)];

           

            img .image = [UIImage imageNamed:@"3.jpg"];

           

            [view.contentView addSubview:img];

3.创建表头表尾 

首先可以给封装成一个方法   并给需要调用的名字传过来

-(UIView *)creatViewWithName : (NSString *)name

{

    UIImageView * imageView = [[[UIImageView alloc]initWithFrame:CGRectMake(0,
0, 320, 120)]autorelease];

   

    imageView .image = [UIImage imageNamed:name];

   

    return imageView;

}
然后 在 tableView 中调用封装方法 设置表头 表尾

 
tableView .tableHeaderView = [self creatViewWithName:@"user_headerbg"];

    tableView .tableFooterView = [self creatViewWithName:@"xzzm_MyStreet_topBg"];

单元格 区 索引 

 

将区头标题 放在数组中 例:

self . arr =
@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#",];

设置区头标题

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

{

    return [NSString stringWithFormat:@"这是第%@
区",_arr[section]];

设置区 索引

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    return _arr;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios开发