自定义tableviewcell为什么要将子视图添加到self satisfied cell.contenview

08-UIKit(UITableTableViewCell、自定义Cell、xcode调试) - 回读 - 推酷
08-UIKit(UITableTableViewCell、自定义Cell、xcode调试) - 回读
1、UITableTableViewCell
[1-TableViewCell-contentView]
1. UITableViewCell : UIView
&&&&&&&&&& -contentView
&&&&&&&&&&&&&&&& -imageView
&&&&&&&&&&&&&&&& -textLabel
&&&&&&&&&&&&&&&& -detailTextLabel
&&&&&&&&&&&&&&&& -自定义的视图
&&&&&&&&&& -accessoryView
&&&&&&&&&&&&&&&& : accessoryType& 使用内置的4种View
&&&&&&&&&&&&&&&& : accessoryView = 其他视图(可以是系统的,也可以是自定义)
[UIFont italicSystemFontOfSize:20];//斜体
lable.textAlignment = NSTextAlignmentC//居中
[cell.contentView addSubview:lable];//在contentView中添加子控件
lable = (UILabel *)[cell.contentView viewWithTag:1];//根据tag属性获取子视图对象
&&&&& [Demo1]
2. tag技术
&&&&& 在一个视图中,所有的子视图对象可以使用tag编号,只要在父视图中的编号不重复,那么可以随时通过父视图的viewWithTag方法获取这个子视图对象。
3. 自定义Cell
&&&&& [2-customCell]
&&&&& 1) 创建一个空的xib文件(command +n -& ios -& user interface -& empty),在xib文件中,拖一个UIView进去,做为将来的Cell
&&&&&&&&&&&&&&&& 要将大小改为freeform(检查器4)
&&&&&&&&&&&&&&& 拖拽一些控件到xib自定义该cell:imageView, Label….
&&&&& 2)创建类MXNewsCell, 继承UITableViewCell
&&&&&&&&&&&&&&&& 让这个xib文件绑定到此类上,绑定的方法是在xib文件上在检查器3设置custom class为MXNewsCell
&&&&& 3)拖拽连线xib文件中的控件到MXNewsCell类中,创建公开的属性,目的是为了给自定义的cell中的控件赋值
&&&&& 4)要在TableViewController中使用这个自定义的cell就要在viewDidLoad方法中注册我们自定义的Cell
// 注册自定义cell
&&& [self.tableView registerNib:[UINib nibWithNibName:@&MXNewsCell& bundle:nil] forCellReuseIdentifier:@&Cell&];
&&&&& 5) 回答三个问题
&&&&&&&&&& 在显示cell内容的方法中返回Cell的问题时,dequeue我们的Cell, 一定会成功(已经注册过), 设置Cell中的相关属性即可
MXNewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
4. 用nib文件构造自定义的静态表
[3-Static-tableView-Xib]
&&&&& 1) 一个xib文件既可以表示一个VC中的View, 还可以加入任何View,比如一个UITableViewCell视图。
&&&&& 2) xib文件中的根视图也可以有多个,也就是说,可以在一个xib文件中加入很多个不同样子的UITableViewCell。
&&&&& 3) xib中的任何视图,都可以绑定对应的类,这个类可以是系统提供的,也可以是用户自定义的。
&&&&& 4) xib中的任何视图对象,都可以用连线的方式变成控制器类中的一个属性。
&&&&& 5)静态表其实就是没有数据模型的一个表,所有的数据都是直接写在控件上
5. TableView数据模型总结
&&&&& MVC, V(xxxx.xib)&&& C(TRMyTableViewController)
&&&&&&&&&& Model:&& NSArray *
&&&&& 5.1& 对象
&&&&&&&&&& TRMyTableViewController.h
&&&&&&&&&& @property (no…)& TRC//Model
&&&&&&&&&& 和TableView的关系:
&&&&&&&&&&&&&&&& 每一行显示这个对象的一个属性
&&&&&&&&&& 一般会使用静态的TableView
&&&&& 5.2& 数组
&&&&&&&&&& 数组--&NSString
&&&&&&&&&& TRMyTableViewController.h
&&&&&&&&&& &-NSArray *
&&&&&&&&&&&&&&&&&&&&& -NSString *
&&&&&&&&&& 和TV的关系:
&&&&&&&&&&&&&&&& 每一行显示数组中的一个字符串
&&&&& 5.3 数组--&对象--&普通属性
&&&&&&&&&& TRTVController.h
&&&&&&&&&&&&&&&& -NSArray *
&&&&&&&&&&&&&&&&&&&&& -TRC
&&&&&&&&&&&&&&&&&&&&&&&&&&& -
&&&&&&&&&&&&&&&&&&&&&&&&&&& -
&&&&&&&&&&&&&&&&&&&&&&&&&&& -
&&&&&&&&&& 对照关系:
&&&&&&&&&&&&&&&& 一行展示一个对象,如果对象比较复杂,可能会用自定义的Cell来展示。
&&&&& 5.4 数组--&对象--&数组
&&&&&&&&&& TRTVController.h
&&&&&&&&&&&&&&&& -NSArray *
&&&&&&&&&&&&&&&&&&&&& -TRArea
&&&&&&&&&&&&&&&&&&&&&&&&&&& -NSString *
&&&&&&&&&&&&&&&&&&&&&&&&&&& -NSArray *subA
&&&&&&&&&& 对照关系:
&&&&&&&&&&&&&&&& 1)可以用分区方式:
&&&&&&&&&&&&&&&&&&&&& 一个objects是一个分区,每一个分区展示subA
&&&&&&&&&&&&&&&& 2)一行显示第一层数组中的一个元素名,点击时推第二个Table,第二个Table中展示第二层数组中的数据。
&&&&& 5.5 多层TableView的展示
&&&&&&&&&& 多层指N层,就是不知道多少层。
&&&&&&&&&& 模型:
&&&&&&&&&&&&&&&& -TRArea& *area
&&&&&&&&&&&&&&&&&&&&& -NSString *name
&&&&&&&&&&&&&&&&&&&&& -NSArray *subArea
&&&&&&&&&&&&&&&&&&&&&&&&&&& -TRArea *area
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -NSString *name
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -NSArray *subArea
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -TRArea *area
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ….(循环)
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
6. Xcode代码调试
&&&&& 6.1 编译错误(程序飘红)
&&&&&&&&&& 1) 保存文件&
&&&&&&&&&&&&&&&& 随时Command+S
&&&&&&&&&& 2) Clean项目(Shift+Command+K)
&&&&&&&&&& 3) 看程序有没有写错
&&&&&&&&&&&&&&&& 大小写,单词拼写,标点符号(中文),语法
&&&&&&&&&& 4) 看大括号,查缩进
&&&&& 6.2 连接错误
&&&&&&&&&& 1) 看错误原因是哪个函数,哪个库没有加进来。
&&&&&&&&&& 2) Xcode出现问题
&&&&&&&&&&&&&&&&&&&&& 修改了系统提供的某个头文件中的内容。
&&&&&&&&&&&&&&&&&&&&& 查看错误信息,其中有一个缓冲区目录(Cache)中的指定的内容删除
&&&&& 6.3 程序逻辑错误(运行时错误)
&&&&&&&&&& 1) 程序直接崩溃
&&&&&&&&&&&&&&&& 查看控制台错误信息, reason(错误原因)很重要
&&&&&&&&&&&&&&&&&&&&& 方法没有找到(@selector(tap), 但是tap方法没有提供)
&&&&&&&&&&&&&&&&&&&&& 下标越界错误
&&&&&&&&&& 2) 程序出现错误的结果
&&&&&&&&&&&&&&&& 不显示, 位置不对,颜色不对:
&&&&&&&&&&&&&&&&&&&&& 有没有addSubview
&&&&&&&&&&&&&&&&&&&&& frame没有有设置,位置是否正确
&&&&&&&&&&&&&&&&&&&&& alpha 是不是0
&&&&&&&&&&&&&&&& &&&&& 是不是几个View放在一个位置
&&&&&&&&&&&&&&&&&&&&& 前景色和背景色是不是一样
&&&&& 6.4 设置断点(breakpoint)
&&&&&&&&&& 主要用来跟踪程序的运行
&&&&&&&&&& 1)在何处设断点
&&&&&&&&&&&&&&&& 二分查找(折半查找)
&&&&&&&&&& 2)确定程序的错误一在定的范围内时,逐行排查
&&&&& 课堂上的内容补上。
&&&&& TMusic完成,各个界面都可以用。界面使用Nib文件来完成。
&&&&& 加TableView显示所有的歌曲
&&&&&&&&&& 数据模型:
&&&&&&&&&& TRMusic
&&&&&&&&&&&&&&&& -NSString *//歌名
&&&&&&&&&&&&&&&& -NSString *//歌手
&&&&&&&&&&&&&&&& -NSString *//专辑名
&&&&&&&&&&&&&&&& -NSTimeI//时长(秒)
&&&&&&&&&& typedef … NSTimeI
注意:在创建弹窗的时候,代理一定要设置,不然点击按钮的方法不会执行
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致主题 : 请问如何捕获作为subview镶嵌在TableViewCell中TextField的值呢
级别: 新手上路
可可豆: 11 CB
威望: 11 点
在线时间: 0(时)
发自: Web Page
来源于&&分类
请问如何捕获作为subview镶嵌在TableViewCell中TextField的值呢&&&
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *CellIdentifier = @&Cell&;
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0,0,300,40)];    textField.tag = indexPath.row;     [cell.contentView addSubview:textField];     return}如上面代码,我在TableViewCell中内陷了一个可供用户输入的TextField,本想通过cell.textField.text来捕捉填入的内容并进行处理的,但是cellForRowAtIndexPath这个方法有个特性,看不到的cell会返回nil,那cell.textField.text就没有值可以捕获得到了,想了一天都没有办法解决,特来求助。
级别: 新手上路
可可豆: 11 CB
威望: 11 点
在线时间: 0(时)
发自: Web Page
我也尝试了为textField添加tag属性值,通过[cell viewWithtag:indexPath.row]来捕获textField中的值,还是由于上面提到的问题无法成功
级别: 新手上路
可可豆: 82 CB
威望: 32 点
在线时间: 37(时)
发自: Web Page
回 1楼(durden) 的帖子
你在cell contenView里 看看
A little bit of progress every day
级别: 新手上路
UID: 178141
可可豆: 231 CB
威望: 158 点
在线时间: 427(时)
发自: Web Page
1、因为UITableView有重用机制,它用两个数组用来存储UITableViewCell,一个是visibleCells,它保存屏幕上看到的cell,滑动tableview时如果一个cell移出屏幕了,它就会被从visibleCells中移出,同时放入到reusebleCells中,所以用cellForRowAtIndexPath是无法获取到。2、按照你的代码,你没有使用dequeueReusableCellWithIdentifier来对cell进行重用,但是也并不会因为这样tableview就会为你alloc一次所有的cell,它依然是只会创建需要在屏幕上显示的cell。3、如果是我,我会用一个数组来保存所有UITextField的值。
级别: 新手上路
UID: 267042
可可豆: 82 CB
威望: 64 点
在线时间: 397(时)
发自: Web Page
试试[cell.contentView viewForTag:indexPath.row];
关注本帖(如果有新回复会站内信通知您)
3*3+1 正确答案:10
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版iOS自定义TableViewCell详解[两种方法] - 博客频道 - CSDN.NET
atany的移动端开发小窝
保持一颗平常心,就像最开始一样
分类:严肃点!学iOS了
今天要跟大家分享的是两种自定义UITableViewCell方法。
一、首先看看效果
1)第一种是通过nib文件加载的方式,在UITableView里面添加自定义的Cell。
2)第二种是代码里面自定义Cell的形式。
两种方式各有各的优点,根据不同的情况进行选择即可。
二、建立项目
1)建立SingleView项目,命名为CustomTableViewCell。
2)完成nib文件配置,View中只有一个UITableView控件,就是我们将要显示的表视图了。
设置style为Grouped,因为我们要把两种自定义Cell的方法用不同的分区显示。
别忘记配置表的代理,在此就不赘述了,有疑问的看看demo就懂了。
三、使用nib文件自定义Cell
1)建立自定义Cell的nib文件,在New File里面选择UserInterface中的Empty,取名为CustomCell。
2)接着,拉出6个label,摆出如下图所示(当然你可以自由发挥),注意中间的”|”也是一个label。为了能够在代码中找到Cell里面的控件,我们还需要设置label的Tag标记。(写死的label就不用了,例如“类型:”)。
3)拖完控件之后,我们需要设置nib文件的控制器,修改File’s Owner的Class为YGViewController(对应自己程序中的表视图控制器),点击File’s Owner,然后在身份检查器中输入YGViewController。
注:一个控制器是可以加载多个nib文件的,这里我们的YGViewController就加载了YGViewController.nib和CustomCell.nib两个文件。
只要配置好输出口和操作的链接,我们就能有条不紊的对多个nib进行操作了。下面设定CustomCell.nib的输出口,取名teaCell。
@property (retain, nonatomic) IBOutlet UITableViewCell *teaC
4)关键代码讲解
YGAViewController.h
#import &UIKit/UIKit.h&
#import &QuartzCore/QuartzCore.h&
#define nameTag
#define classTag
#define stuNumberTag
#define imageTag
#define nameFontSize
#define fontSize
//老师,对应的nib文件里面label的tag
#define teaNameTag
#define teaTypeTag
#define teaOfficeTag
@interface YGViewController : UIViewController &UITableViewDelegate,UITableViewDataSource&
@property (retain,nonatomic) NSArray *stuA//学生资料
@property (retain,nonatomic) NSArray *teaA//老师资料
@property (retain, nonatomic) IBOutlet UITableViewCell *teaC
YGAViewController.m
初始化数据:
//初始化老师数据
NSDictionary *tDic1 = [[NSDictionary alloc]initWithObjectsAndKeys:@&史小强&,@&name&,@&讲师&,@&type&, @&C406&, @&office&,nil];
NSDictionary *tDic2 = [[NSDictionary alloc]initWithObjectsAndKeys:@&李永乐&,@&name&,@&教授&,@&type&, @&D011&, @&office&,nil];
_teaArray = [[NSArray alloc]initWithObjects:tDic1,tDic2, nil];
Cell的生成方法,此时section等于2,调用customCellByXib方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *
if([indexPath section] == 1){
//通过代码自定义cell
cell = [self customCellWithOutXib:tableView withIndexPath:indexPath];
//通过nib自定义cell
cell = [self customCellByXib:tableView withIndexPath:indexPath];
assert(cell != nil);
//通过nib文件自定义cell
-(UITableViewCell *)customCellByXib:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath{
static NSString *customXibCellIdentifier = @&CustomXibCellIdentifier&;//建立标志符
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:customXibCellIdentifier];
if(cell == nil){
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@&CustomCell& owner:self options:nil];//加载nib文件
if([nib count]&0){
cell = _teaC
assert(NO);//读取文件失败
NSUInteger row = [indexPath row];//获得当前行
NSDictionary *dic
= [_teaArray objectAtIndex:row];//对应行的数据
((UILabel *)[cell.contentView viewWithTag:teaNameTag]).text = [dic objectForKey:@&name&];
((UILabel *)[cell.contentView viewWithTag:teaTypeTag]).text = [dic objectForKey:@&type&];
((UILabel *)[cell.contentView viewWithTag:teaOfficeTag]).text = [dic objectForKey:@&office&];
四、在代码里面自定义cell
配置的步骤和上面一样,主要区别在于需要自己自定义控件来是实现cell。
YGAViewController.m
//初始化学生数据(class,name,stuNumber,image)
NSDictionary *dic1 = [[NSDictionary alloc]initWithObjectsAndKeys:@&一班&,@&class&,@&李秋&,@&name&,@&&,@&stuNumber&,@&1.jpg&,@&image&,nil];
NSDictionary *dic2 = [[NSDictionary alloc]initWithObjectsAndKeys:@&二班&,@&class&,@&张亮&,@&name&,@&&,@&stuNumber&,@&2.jpg&,@&image&,nil];
NSDictionary *dic3 = [[NSDictionary alloc]initWithObjectsAndKeys:@&三班&,@&class&,@&杨光&,@&name&,@&&,@&stuNumber&,@&3.jpg&,@&image&,nil];
NSDictionary *dic4 = [[NSDictionary alloc]initWithObjectsAndKeys:@&四班&,@&class&,@&atany&,@&name&,@&&,@&stuNumber&,@&4.jpg&,@&image&,nil];
_stuArray = [[NSArray alloc]initWithObjects:dic1, dic2, dic3, dic4, nil];
自定义Cell:
-(UITableViewCell *)customCellWithOutXib:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath{
//定义标识符
static NSString *customCellIndentifier = @&CustomCellIndentifier&;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:customCellIndentifier];
//定义新的cell
if(cell == nil){
//使用默认的UITableViewCell,但是不使用默认的image与text,改为添加自定义的控件
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:customCellIndentifier];
CGRect nameRect = CGRectMake(88, 15, 70, 25);
UILabel *nameLabel = [[UILabel alloc]initWithFrame:nameRect];
nameLabel.font = [UIFont boldSystemFontOfSize:nameFontSize];
nameLabel.tag = nameT//设置tag,以便后面的定位
nameLabel.textColor = [UIColor brownColor];
[cell.contentView addSubview:nameLabel];
CGRect classTipRect = CGRectMake(88, 40, 40, 14);
UILabel *classTipLabel = [[UILabel alloc]initWithFrame:classTipRect];
classTipLabel.text = @&班级:&;
classTipLabel.font = [UIFont boldSystemFontOfSize:fontSize];
[cell.contentView addSubview:classTipLabel];
CGRect classRect = CGRectMake(135, 40, 40, 14);
UILabel *classLabel = [[UILabel alloc]initWithFrame:classRect];
classLabel.tag = classT
classLabel.font = [UIFont boldSystemFontOfSize:fontSize];
[cell.contentView addSubview:classLabel];
CGRect stuNameTipRect = CGRectMake(88, 60, 40, 12);
UILabel *stuNameTipLabel = [[UILabel alloc]initWithFrame:stuNameTipRect];
stuNameTipLabel.text = @&学号:&;
stuNameTipLabel.font = [UIFont boldSystemFontOfSize:fontSize];
[cell.contentView addSubview:stuNameTipLabel];
CGRect stuNameRect = CGRectMake(135, 60, 150, 14);
UILabel *stuNameLabel = [[UILabel alloc]initWithFrame:stuNameRect];
stuNameLabel.tag = stuNumberT
stuNameLabel.font = [UIFont boldSystemFontOfSize:fontSize];
[cell.contentView addSubview:stuNameLabel];
CGRect imageRect = CGRectMake(15, 15, 60, 60);
UIImageView *imageView = [[UIImageView alloc]initWithFrame:imageRect];
imageView.tag = imageT
//为图片添加边框
CALayer *layer = [imageView layer];
layer.cornerRadius = 8;//角的弧度
layer.borderColor = [[UIColor whiteColor]CGColor];
layer.borderWidth = 1;//边框宽度
layer.masksToBounds = YES;//图片填充边框
[cell.contentView addSubview:imageView];
//获得行数
NSUInteger row = [indexPath row];
//取得相应行数的数据(NSDictionary类型,包括姓名、班级、学号、图片名称)
NSDictionary *dic = [_stuArray objectAtIndex:row];
//设置图片
UIImageView *imageV = (UIImageView *)[cell.contentView viewWithTag:imageTag];
imageV.image = [UIImage imageNamed:[dic objectForKey:@&image&]];
//设置姓名
UILabel *name = (UILabel *)[cell.contentView viewWithTag:nameTag];
name.text = [dic objectForKey:@&name&];
//设置班级
UILabel *class = (UILabel *)[cell.contentView viewWithTag:classTag];
class.text = [dic objectForKey:@&class&];
//设置学号
UILabel *stuNumber = (UILabel *)[cell.contentView viewWithTag:stuNumberTag];
stuNumber.text = [dic objectForKey:@&stuNumber&];
//设置右侧箭头
cell.accessoryType = UITableViewCellAccessoryDisclosureI
1、在cell=nil里面摆好控件的位置,设置好相应的tag,然后在设置数据时,通过cell.contentViewviewWithTag:Tag 的形式定位到对应的Cell里面的tag。
2、通过NSUInteger row = [indexPath row]的到当前是哪一行,NSDictionary *dic = [_stuArrayobjectAtIndex:row];得到那一行的数据。
demo下载地址:http://download.csdn.net/detail/yang9667
【传送门】想系统学习UITableView SDK代理方法可以看这篇文章:UITableView SDK委托方法详解
杨光(atany)原创,转载请注明博主与博文链接,未经博主允许,禁止任何商业用途。
博文地址:
博客地址:
—— by atany
本文遵循“”创作公用协议
yang8456211
排名:千里之外
(16)(4)(2)(0)(3)(3)iOS学习:自定义tableViewCell
iOS学习:自定义tableViewCell
iOS学习:自定义tableViewCell
发表于2年前( 17:33)&&
阅读(863)&|&评论()
1人收藏此文章,
可用Xib创建一个属于自己的Cell,可添加各种控件试图,适合复杂的表格,尤其是需要获取整张表格的内容时,本文讲述如何自定义表格,并可修改表上的数据,点击按钮事件等,并获取到整张表的内容。
效果如下图:可触发按钮事件
1、创建一个Empty Application
2、新建一个TableViewController,命名为MyTable
2.1在AppDelegate.h中添加@class 和property
123456789101112#import &UIKit/UIKit.h&@class MyT@interface AppDelegate : UIResponder &UIApplicationDelegate&@property (strong, nonatomic) UIWindow *//声明@property (strong, nonatomic) MyTable *MyTableV@end 2.2在AppDelegate.m的
didFinishLaunchingWithOptions方法中
12345678910111213141516- (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 =&&&&&&&&&[self.window makeKeyAndVisible];&&&&return YES;}
2.3、在MyTable的viewWillAppear方法中
12345-(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方法
12345678910111213- (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方法
1234567891011121314151617181920212223242526272829303132333435363738- (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 = UITableViewCellAccessoryDisclosureI&&&&&&&&&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 =&&&&return }
发表评论:
馆藏&18954
TA的最新馆藏

我要回帖

更多关于 self size cell 的文章

 

随机推荐