1.storyboard创建tableview,拉进controller中建立链接
2.storyboard建立约束:在连接器上将dataSource delegate拉到controller view的小按钮上
在controller.h上继承UIViewController<UITableViewDataSource,UITableViewDelegarte>
3.在controller.m中添加方法:
numberOfRowsInSection(注意这里counts是否真的有值)
cellForRowAtIndewPat
4.添加tableViewCell:
(1)在storyboard的tableview上拉上tableViewCell,设置好样式
(2)建一个tableviewcell的类,把样式的控件链接上.h
(3)storyboard上Custom Class改变指向类,Identifier上命名识别名称
5.在代码中指向cell:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIden = @"deviceListCell";
DeviceListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden forIndexPath:indexPath];
cell.deviceName.text = @"test";//for test
return cell;
}
6.调整cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 80;
}
二.纯代码自定义cell
参考:http://blog.youkuaiyun.com/u012350430/article/details/51181728
重写-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier:
//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;
}
本文详细介绍使用Storyboard创建TableView及自定义TableViewCell的过程,并提供纯代码方式自定义TableViewCell的方法。包括如何在Storyboard中设置TableView和TableViewCell,如何通过代码指定Cell的高度和内容等。
4697

被折叠的 条评论
为什么被折叠?



