http://my.oschina.net/joanfen/blog/137601

效果如下图:可触发按钮事件

1、创建一个Empty Application

2、新建一个TableViewController,命名为MyTable

2.1在AppDelegate.h中添加@class 和property

1
2
3
4
5
6
7
8
9
10
11
12
#import <UIKit/UIKit.h>
 
@class MyTable;
 
@interface AppDelegate : UIResponder <UIApplicationDelegate>
 
@property (strong, nonatomic) UIWindow *window;
 
//声明
@property (strong, nonatomic) MyTable *MyTableView;
 
@end

2.2在AppDelegate.m的 didFinishLaunchingWithOptions方法中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
     
    self.window.backgroundColor = [UIColor whiteColor];
     
    _MyTableView = [[MyTable alloc] initWithNibName:@"MyTable" bundle:nil];
     
    //创建一个navigationController,也可不创建,直接将window的rootViewController设定为MyTableView,此处创建是为了让程序有NavigationController的属性,方便Push视图
    UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:self.MyTableView];
     
    self.window.rootViewController = nv;
     
    [self.window makeKeyAndVisible];
    return YES;
}

2.3、在MyTable的viewWillAppear方法中

1
2
3
4
5
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.title = @"自定义Table";
}

3、新建一个UITableViewCell类,命名为MyCell

4、新建一个View,命名与上面的相同(也可不同,只是命名相同更加方便)

4.1、将此View中的View删除,拖一个TableViewCell进来,将此tableViewCell的Custom Class改成3中新建的类MyCell

4.2.1、在cell中添加一个Label,创建映射

4.2.2、在Cell中添加一个TextField,创建映射

4.3.3、在Cell中添加一个Switch,创建映射

4.3.4、在Cell中添加一个segment,创建映射

5、在MyTable.m中,补充tableViewDataSource方法

1
2
3
4
5
6
7
8
9
10
11
12
13
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
 
    // Return the number of sections.
    return 1;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 
    // Return the number of rows in the section.
    return 4;
}

补充CellForRow方法

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
38
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    //自定义cell
    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell ==nil) {
        //加载MyCell.xib文件,此处loadNibNamed后面的参数CellIdentifier必须与MyCell.xib文件名相同,否则会无法加载,报错崩溃
        NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
        cell = (MyCell *)[nibArray objectAtIndex:0];
         
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     
    NSArray *array = [NSArray arrayWithObjects:@"姓名",@"性别",@"学历",@"保险", nil];
     
    cell.title.text = [array objectAtIndex:indexPath.row];
     
    //根据行数来确定每行内容
    if (indexPath.row == 0||indexPath.row==2) {
        cell.textField.hidden = NO;
        cell.swich.hidden = YES;
        cell.segment.hidden = YES;
    }
    else if(indexPath.row == 1){
        cell.textField.hidden = YES;
        cell.segment.hidden = NO;
        cell.swich.hidden = YES;
    }
    else if(indexPath.row == 3)
    {
        cell.textField.hidden = YES;
        cell.swich.hidden = NO;
        cell.segment.hidden = YES;
    }
    //设置TextField代理
    cell.textField.delegate = self;
    return cell;
}

 

自定义tableViewCell的更多相关文章

  1. 自定义 TableViewCell 的分割线

    刚开始自定义 tableViewCell 的时候,用的是直接在 cell 上加一张 imageView 的方法,如果在点击 cell 的时候有页面的跳转,这样做没什么问题,但是,如果在点击 cell ...

  2. IOS开发自定义tableviewcell的注意点😄

    自定义tableviewcell 1.xib,nib拖控件:awakefromnib: 设置2,不拖控件:- (instancetype)initWithStyle:(UITableViewCellS ...

  3. 自定义TableViewCell 的方式实现自定义TableView(带源码)

    转载于:http://www.cnblogs.com/macroxu-1982/archive/2012/08/30/2664121.html 实现的效果 实现过程 Step One 创建 自定义Ta ...

  4. TableView,自定义TableViewCell

    自定义Table 原理: http://blog.jobbole.com/67272/ http://www.cnblogs.com/wangxiaofeinin/p/3532831.html 补充: ...

  5. 在自定义TableViewCell类里面添加按钮事件触发不了的一些实践

    我的自定义cell上面有5个控件,分别是一个背景的UIImageView,一个专辑的UIImageView(上面加了一个播放的button),一个专辑名字的UIImageView(上面加了显示标题的U ...

  6. [iOS基础控件 - 6.6] 展示团购数据 自定义TableViewCell

    A.需求 1.头部广告 2.自定义cell:含有图片.名称.购买数量.价格 3.使用xib设计自定义cell,自定义cell继承自UITableViewCell 4.尾部“加载更多按钮”,以及其被点击 ...

  7. OC开发_代码片段——使用Xib自定义tableViewCell

    一.实现步骤 1.新建一个XIB文件:描述cell——tableCell.xib 2.新建UITableViewCell的子类,也就是cell文件:封装XIB内部的所有东西——TestCell.m \ ...

  8. iOS开发:自定义tableViewCell处理的问题

    还在适配iOS6,索性下一个版本不适配了~~~~~ 问题: *** Assertion failure in -[ PCDiaryDetailReplyCell layoutSublayersOfLa ...

  9. 自定义tableviewCell的分割线

    第一种:addsubview UIView *line = [[UIView alloc]initWithFrame:CGRectMake(10, cellH-0.5, DEVW-10, 0.5)]; ...

随机推荐

  1. Power-BI费用分析

    费用分析主要从财务三大费用入手,剖析费用的结构.用途.占用等情况,从三大费用到明细费用.部门.职员的层层钻取,从而有效地进行费用管理和控制.Power-BI前端展示:图1<ignore_js_o ...

  2. Android --ListView使用ArrayAdapter

    1.继承ArrayAdapter public class TimerDataAdapter extends ArrayAdapter<TimerDataListItem> { //数据I ...

  3. 字符串拷贝函数strcpy写法_转

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...

  4. ios数字转emoj表情

    +(NSString *)convertSimpleUnicodeStr:(NSString *)inputStr{ ,); UTF32Char inputChar = ; // unicodeInt ...

  5. iOS 归档

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  6. Java基础之写文件——使用多个视图缓冲区(PrimesToFile2)

    控制台程序.本例将对应于每个素数的数据以三个连续数据项的形式写入: 1.以二进制值表示的字符串长度值(最好是整型,但本例使用double类型): 2.素数值的字符串表示”Prime=nnn“,其中数字 ...

  7. CommonJS规范

    CommonJS是一种规范,NodeJS是这种规范的实现.CommonJS是一 个不断发展的规范,计划将要包括如下部分: Modules Binary strings and buffers Char ...

  8. [转] HashMap的工作原理

    HashMap的工作原理是近年来常见的Java面试题.几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道Hashtable和HashMap之间的区别,那么为何这道面试题如此 ...

  9. 查看真机的系统中sdk的版本

    1.adb devices 确保连接上了真机 2.adb shell 进入android系统 3.进入system目录下 4.查看build.prop文件 cat build.prop

  10. C++之路起航——标准模板库(set)

    set(集合):http://baike.baidu.com/link?url=cb68AB-3qfEK8RoaGHJFClb4ZiWpJfc32lPOLtaNUrdxntFC738zCZsCiUlf ...