自定义TableViewCell

本文详细介绍了UITableView的基本使用方法,包括创建和配置TableView、设置单元格样式、定义不同高度的单元格、实现多选功能、编辑和删除操作等内容。

1   //创建tableView

    UITableView *myTable =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)   style:UITableViewStylePlain];

    myTable.backgroundColor =[UIColor clearColor];

//调代理

    myTable.delegate =self;

//调委托

    myTable.dataSource =self;

//选中cell,调下面方法

    myTable.separatorStyle =UITableViewCellSeparatorStyleNone;

    [self.view addSubview:myTable];

 

//返回数据中有几组 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

  return 1;  

}

 //返回数据中每个组里有几行 

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

{

    return 2;

}

//返回数据中每一行所包含的信息

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

{

    static NSString *identifierStr =@"idetifier";

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifierStr];

 

     if (!cell)

    {

        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierStr];

    }

   return  cell;

}

 

2  NSIndexPath用来保存哪一组的哪一行.

      [ indexPath indexAtPosition: 0 ]哪一组

      [ indexPath indexAtPosition: 1 ]哪一行

 

3  UITableViewCell包含图像,文本等.

      NSString *CellIdentifier = [ [ NSString alloc ] initWithString: @"Frank" ];

      UITableViewCell *cell =  [ [ UITableViewCell alloc] initWithFrame: CGRectZero reuseIdentifier: CellIdentifier ];

 

4  为每一个cell设置不同的风格

     (1) 显示文本: cell.text = @"Frank's Table Cell";

     (2) 对齐: cell.textAlignment = UITextAlignmentLeft;

             UITextAlignmentLeft 默认是左对齐

             UITextAlignmentRight 右对齐

             UITextAlignmentCenter 中对齐

     (3) 字体和尺寸:

            #import <UIKit/UIFont.h>

             UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 18.0 ]; 

             cell.font = myFont;

     //系统字体

           UIFont *mySystemFont = [ UIFont systemFontOfSize: 12.0 ];

           UIFont *myBoldSystemFont = [ UIFont boldSystemFontOfSize: 12.0 ];

           UIFont *myItalicSystemFont = [ UIFont italicSystemFontOfSize: 12.0 ];

     (4) 颜色

            #import <UIKit/UIColor.h>

          //文本颜色

              cell.textColor = [ UIColor redColor ];

         //当前选择项的颜色

              cell.selectedTextColor = [ UIColor blueColor ];

      (5) 图像

           //从你应用程序目录下的文件创建一个image

              cell.image = [ UIImage imageNamed: @"cell.png" ];

          //当前选中项的图形

              cell.selectedImage = [ UIImage imageNamed: @"selected_cell.png" ];

 

5  可修改table保准行高来适应你的图形高度

- (id)init 

{

    self = [ super init ];

    if (self != nil) 

    {

        self.tableView.rowHeight = 65; 

    }

    return self; 

}

 

6 为每一个cell定义不同的高度

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

{

    if ([ indexPath indexAtPosition: 1 ] == 0)

        return 65.0; 

    else

        return 40.0; 

}

 

7 选中项的风格

      cell.selectionStyle = UITableViewCellSelectionStyleBlue;

          UITableViewCellSelectionStyleBlue 默认选中项是蓝色

          UITableViewCellSelectionStyleGray 灰色

          UITableViewCellSelectionStyleNone 没有变化

 

8  标签 (labels)

     UILabel *label = [ [ UILabel alloc ] initWithFrame: CGRectMake(100.0, 0.0, 50.0, 50.0) ];

     label.text = @"Label Text";

     label.textAlignment = UITextAlignmentLeft;

     label.textColor = [ UIColor redColor ];

     label.font = [ UIFont fontWithName: @"Arial" size: 10.0 ];

   标签label可以设置文本阴影,甚至可以定义阴影的偏移:

     label.shadowColor = [ UIColor grayColor ];

     label.shadowOffset = CGSizeMake(0, -1);

   高亮是的颜色:

     label.highlightedTextColor = [ UIColor blackColor ];

   标签的背景色:

     label.backgroundColor = [ UIColor blueColor ];

   把标签加到cell里

     [ cell addSubview: label ];

 

9 附件

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

      Style

      Description

      UITableViewCellAccessoryNone

   没有附件

      UITableViewCellAccessoryDisclosureIndicator

   黑色向右的箭头

      UITableViewCellAccessoryDetailDisclosureButton

   蓝色附件按钮

      UITableViewCellAccessoryCheckmark

   复选框,支持选择

 

10 实现多选

- (void)tableView:(UITableView *)tableView

        didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

{

    NSLog(@"Selected section %d, cell %d", 

        [ indexPath indexAtPosition: 0 ], [ indexPath indexAtPosition: 1 ]); 

    //获的当前选择项

    UITableViewCell *cell = [ self.tableView cellForRowAtIndexPath: indexPath ]; 

    //显示复选框

    if (cell.accessoryType == UITableViewCellAccessoryNone)

        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    else

        cell.accessoryType = UITableViewCellAccessoryNone; 

}

 

11 编辑和删除

   在允许用户删除和编辑的时候,每一个cell左边会显示一个红色删除图标

      [ self.tableView setEditing:YES animated:YES ];

   关闭编辑的时候,table顶部会显示一个Edit导航条

      [ self.tableView setEditing: NO animated: YES ];

   在编辑过程中,如果用户要删除该项,会弹出一个删除确认框. 确认后调UITableViewDataSource类的 commitEditingStyle方法来通知你的应用程序, 然后你可以从你的底层数据源里删除该项,并通知table view删除该行.

- (void)tableView:(UITableView *)tableView

    commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

    forRowAtIndexPath:(NSIndexPath *) indexPath 

{

    if (editingStyle == UITableViewCellEditingStyleDelete) 

    {

        NSLog(@"Deleted section %d, cell %d", [ indexPath indexAtPosition: 0 ], [ indexPath indexAtPosition: 1 ]);

        NSMutableArray *array = [ [ NSMutableArray alloc ] init ];

        [ array addObject: indexPath ];

        [ self.tableView deleteRowsAtIndexPaths: array

            withRowAnimation: UITableViewRowAnimationFade 

        ];

     }

}

通过传递一个数组给deleteRowsAtIndexPaths方法, 可以删除一行或多行.

//  withRowAnimation至此下列预定义的删除动画

       Animation

       Description

       UITableViewRowAnimationFade

       Cell fades out

       UITableViewRowAnimationRight

       Cell slides out from right

       UITableViewRowAnimationLeft  

       Cell slides out from left

       UITableViewRowAnimationTop

       Cell slides out to top of adjacent cell

       UITableViewRowAnimationBottom

       Cell slides out to bottom of adjacent cell

 

12 重新加载表

      [ self.tableView reloadData ];

转载于:https://www.cnblogs.com/luoyubuku/p/3824569.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值