zoukankan      html  css  js  c++  java
  • IOS 表视图(UITableVIew)的使用方法(4)自定义表视图单元

    UITableViewCell的自定义往往需要自建一个UITableViewCell的子类后进行作业。开发者可以选择通过xib或者直接在UITableViewCell的布局中进行UITableViewCell的自定义。这节会采用更直观易懂的xib方式。

    (1)新建一个UITableViewCell的子类取名为CustomCell,并且为CustomCell类勾选“xib文件的关联”,CustomCell的xib设计如图所示:

    注意:灰色的右箭头不是我们拖入的控件,而是UITableView的属性配置。在新建俩个类文件HBCustomCell.h和HBCustomCell.m,在CumtomView.xib中,把tableViewCell的类选定为HBCustomCell,如图:

    在storyboard 中,在tableviewcontroller中,点击tableviewCell,让其类也写成HBCustomCell

    (2)完成CustomCell的声明和基本内容撰写。

    1 #import <UIKit/UIKit.h>
    2 
    3 @interface HBCustomCell : UITableViewCell
    4 @property (weak, nonatomic) IBOutlet UILabel *playerNumber;
    5 @property (weak, nonatomic) IBOutlet UILabel *playerName;
    6 @property (weak, nonatomic) IBOutlet UILabel *playerRole;
    7 @property (weak, nonatomic) IBOutlet UIImageView *playerPhoto;

    (3)完成TableViewController的声明和基本内容撰写  

       新建一个继承自SimpleTabelViewController的子类名为CustomLayoutViewController。本节考虑让UITableViewCell 可以响应点击事件,并且自定义一切UITableViewCell的信息,所以需要重写initUI方法。

    -(void)initUI
    {
        [super initUI];
        
        //表单元可点击
        self.tableView.allowsSelection = YES;
        //表单元高度
        self.tableView.rowHeight = 44.0f;
    }

    (4)完成自定义行单元的现实工作

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"customTableViewCellId";
        
        HBCustomCell *cell = (HBCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil)
        {
            NSArray *arrNib=[[NSBundle mainBundle]loadNibNamed:@"CustomView" owner:self options:nil];
            if(arrNib)
            {
                //第一个元素就是需要的UITableViewCell
                cell=(HBCustomCell *)[arrNib objectAtIndex:0];
            }
        }
        
        //配置UITableViewCell
        HBPlayerInfo *onePlayer=[self.datasource objectAtIndex:indexPath.row];
        if(onePlayer)
        {
            cell.playerName.text=onePlayer.name;
            cell.playerRole.text=onePlayer.role;
            cell.playerNumber.text=[NSString stringWithFormat:@"No.%@",onePlayer.number];
            cell.playerPhoto.image=[UIImage imageNamed:@"gaolin.jpeg"];
        }
        return cell;
    }

    这里对于xib的读取并不像UIViewController提供的接口那样,而是使用最直接的xib读取方式loadNibNamed,这个方法成功后往往会得到一个xib中所有主要元素的列表

    (5)完成点击事件的响应

    UITableViewCell收到事件后会通过代理回调函数通知到代理对象处,所以开发者需要实现代理函数如下:

    #pragma mark -
    #pragma mark Table View delegate
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //反选
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        //做些响应的事情
        HBPlayerInfo *onePlayer = [self.datasource objectAtIndex:indexPath.row];
        NSString *str=[NSString stringWithFormat:@"我是:%@", onePlayer.name];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"嗨!" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
    }
    
    //蓝色右箭头的点击事件
    -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
    {
        [self tableView:tableView didSelectRowAtIndexPath:indexPath];
    }

    运行程序如下:

  • 相关阅读:
    oracle trunc()函数的用法——日期、数字
    ORACLE定时任务时间间隔设置
    Oracle JOB 间隔时间详解
    "规格"与"数量"的英文缩写是什么
    “金额”“合计”用英语怎么说?有什么区别么?
    关于狼性的团队励志名言警句
    5篇关于职场技巧的励志文章
    菜单权限分配源码奉送V2.0
    安装 SQL Server 客户端驱动程序
    一步步开发自己的博客 .NET版(9、从model first替换成code first 问题记录)
  • 原文地址:https://www.cnblogs.com/haibosoft/p/3670281.html
Copyright © 2011-2022 走看看