自定义UITableViewCell

from:http://blog.csdn.net/u012350430/article/details/51181728

自定义UITableViewCell

创建一个TableViewController类继承于UITableViewController,创建一个TableViewCell类继承于UITableViewCell。

AppDelegate.m编写代码如下

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    TableViewController *view = [[TableViewController alloc] init];
    self.window.rootViewController = view;
    [self.window makeKeyAndVisible];
    return YES;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

自定义Cell,在TableViewCell.m中编写代码如下

//cell自定义用的是-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier方法
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //这里顺便介绍小UIButton的创建
        //设置button的类型是UIButtonTypeRoundedRect
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        //设置button的frame
        button.frame = CGRectMake(20, 20, 50, 50);

        //button正常状态title设置为Yes,被选择状态title设置为No
        [button setTitle:@"Yes" forState:UIControlStateNormal];
        [button setTitle:@"No" forState:UIControlStateSelected];

        //设置button响应点击事件的方法是buttonPressed:
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        //添加到cell
        [self addSubview:button];

        //创建imageView添加到cell中
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Totoro副本"]];
        imageView.frame = CGRectMake(150, 20, 150, 100);
        [self addSubview:imageView];

    }
    return self;
}

//buttonPressed:方法
-(void)buttonPressed:(UIButton *)button
{
    //实现按钮状态的切换
    button.selected = !button.selected;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

TableViewController.m中编写如下代码

//用来指定表视图的分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    //分区设置为1
    return 1;
}

//用来指定特定分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    //设置为20行
    return 20;
}

//配置特定行中的单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {

        //单元格样式设置为UITableViewCellStyleDefault
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

//设置单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat
{
    //这里设置成150
    return 150;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

运行代码,结果如下图所示 

时间: 2024-08-28 01:27:17

自定义UITableViewCell的相关文章

【转】iOS 通过xib自定义UITableViewCell【原创】

原文网址:http://blog.it985.com/9683.html 在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观.并且要不停的调整位置,字体什么的.这时,我们可以通过在tableViewCell的xib上搭建会更加直观,有效提高开发效率.首先,在我们创建了工程之后,新建XIB的cell.command+n,选择Cocoa Touch Class然后选择UITableViewCell类型,同时钩上Also Create xib File之后,在对应的c

ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局 一.实现效果 二.使用纯代码自定义一个tableview的步骤 1.新建一个继承自UITableViewCell的类 2.重写initWithStyle:reuseIdentifier:方法 添加所有需要显示的子控件(不需要设置子控件的数据和frame,  子控件要添加到contentView中

iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文件控件tag值操作 数据模型部分: YYtg.h文件 // // YYtg.h // 01-团购数据显示(没有配套的类) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. //

新手教程之使用Xib自定义UITableViewCell

新手教程之使用Xib自定义UITableViewCell 前言 首先:什么是UITableView?看图 其次:什么是cell? 然后:为什么要自定cell,UITableView不是自带的有cell么? 因为在日常开发中,系统自带的cell满足不了客户和开发人员的需求(并且每个cell中的内容\大小\样式相同),我们就需要自定义cell来实现更加优化的功能.比如下面这种 最后:怎么自定义cell? 1.创建一个新的项目,在storyboard中拖入两个imageView,两个label   2

自定义UITableViewCell实现左滑动多菜单功能LeftSwipe

今天愚人节,小伙们,愚人节快乐! 实现一个小功能,滑动菜单,显示隐藏的功能菜单, 先上图:                       这里尝试用了下使用三个方式来实现了这个功能: 1.使用自定义UITableViewCell + UISwipeGestureRecognizer + 代理 实现: 2.使用自定义UITableViewCell + UIPanGestureRecognizer + 代理 实现: 3.使用自定义UITableViewCell + UISwipeGestureReco

IOS 自定义UITableViewCell 子控件无法接受到事件

该问题浪费了3个小时的时间 一 问题描述 自定义UITableViewCell,Cell 中有两个UIImageView 子控件,自控都需要实现双击,让图片全局展示. 二 错误代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    BZEffectImgViewCell *cell = [BZEffectImgViewCell ce

自定义UITableViewCell:Cell高度、分割线、间距等

UITableView的强大更多程度上来自于可以任意自定义UITableViewCell单元格. 通常,UITableView中的Cell是 动态的,在使用过程中,会创建一个Cell池,根据每个cell的高度(即tableView:heightForRowAtIndexPath:返回 值),以及屏幕高度计算屏幕中可显示几个cell.而进行自定义TableViewCell无非是采用代码实现或采用IB编辑nib文件来实现两种方式, 本文主要收集代码的方式实现各种cell自定义. 如何动态调整Cell

转:自定义UITableViewCell:Cell高度、分割线、间距等

UITableView的强大更多程度上来自于可以任意自定义UITableViewCell单元格. 通常,UITableView中的Cell是 动态的,在使用过程中,会创建一个Cell池,根据每个cell的高度(即tableView:heightForRowAtIndexPath:返回 值),以及屏幕高度计算屏幕中可显示几个cell.而进行自定义TableViewCell无非是采用代码实现或采用IB编辑nib文件来实现两种方式, 本文主要收集代码的方式实现各种cell自定义. 如何动态调整Cell

用xib自定义UITableViewCell

1.文件结构: 2. 先创建一个xib文件,删除原有的view,添加一个TableViewCell控件. 3.ModelTableViewController.m文件 1 #import "ModelTableViewController.h" 2 #import "Cell.h" 3 4 5 @interface ModelTableViewController () 6 7 @end 8 9 @implementation ModelTableViewContr