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. UITableView + UISearchBar 实现搜索功能

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

  2. 说说 PWA 和微信小程序--Progressive Web App

    作者:云图图链接:https://zhuanlan.zhihu.com/p/22578965来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 利益相关:微信小用户,谷歌小 ...

  3. urllib下载文件

    import urllib 1.用urlib.urlretrieve f = urllib.urlretrieve('http://www.baidu.com/img/bdlogo.gif','/tm ...

  4. R12.2.0 buildStage 运行结果

    # ./buildStage.sh Copyright (c) , Oracle Corporation Redwood Shores, California, USA Oracle E-Busine ...

  5. Protostuff序列化

    前言: Java序列化是Java技术体系当中的一个重要议题,序列化的意义在于信息的交换和存储,通常会和io.持久化.rmi技术有关(eg:一些orm框架会要求持久化的对象类型实现Serializabl ...

  6. JDK JVM

  7. Linux内核源码分析方法

    一.内核源码之我见 Linux内核代码的庞大令不少人“望而生畏”,也正因为如此,使得人们对Linux的了解仅处于泛泛的层次.如果想透析Linux,深入操作系统的本质,阅读内核源码是最有效的途径.我们都 ...

  8. ${pageContext.request.contextPath} JSP取得绝对路径

    一.问题     JSP中究竟采用绝对路径还是采用相对路径随着所采用技术的越来越复杂,这个问题也变得越来越难以解决. 1)采用相对路径遇到的问题      相对路径固然比较灵活,但如果想复制页面内的代 ...

  9. zabbix监控企业esxi虚拟机

    zabbix监控企业esxi虚拟机 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我来公司有一段时间了,最近也发现模型部有测试和开发反应某台机器登陆不上去了,结果登陆esxi服务器 ...

  10. 资源Createwindow,对应标识符,绑定窗口

    问? 定义一个CEdit cedit1:怎么和IDC_EDIT1 关联,可以在CEdit.Create()里传进去或者在DoDataExchange()里面绑定,是不是一定要先弄出个IDC_EDIT1 ...