iOS开发-在表单元中添加子视图
#import <UIKit/UIKit.h> @interface NameAndColorCellTableViewCell : UITableViewCell @property(copy,nonatomic) NSString *name;
@property(copy,nonatomic) NSString *color; @end
NameAndColorCellTableViewCell.h
//
// NameAndColorCellTableViewCell.m
// Tabel Cells
//
// Created by Jierism on 16/7/21.
// Copyright © 2016年 Jierism. All rights reserved.
// #import "NameAndColorCellTableViewCell.h" @interface NameAndColorCellTableViewCell () // 定义两个属性变量
@property(strong,nonatomic) UILabel *nameLabel;
@property(strong,nonatomic) UILabel *colorLabel; @end @implementation NameAndColorCellTableViewCell - (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; // Configure the view for the selected state
} - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// 初始化代码
// 往表单元里面添加子视图
// 这里在每个表单元里添加了四个Label
CGRect nameLabelRect = CGRectMake(, , , );
UILabel *nameMaker = [[UILabel alloc] initWithFrame:nameLabelRect];
nameMaker.textAlignment = NSTextAlignmentRight; // 右对齐
nameMaker.text = @"Name:";
nameMaker.font = [UIFont boldSystemFontOfSize:];
[self.contentView addSubview:nameMaker]; CGRect colorLabelRect = CGRectMake(, , , );
UILabel *colorMaker = [[UILabel alloc] initWithFrame:colorLabelRect];
colorMaker.textAlignment = NSTextAlignmentRight;
colorMaker.text = @"Color:";
colorMaker.font = [UIFont boldSystemFontOfSize:];
[self.contentView addSubview:colorMaker]; CGRect nameValueRect = CGRectMake(, , , );
self.nameLabel = [[UILabel alloc] initWithFrame:nameValueRect];
[self.contentView addSubview:_nameLabel]; CGRect colorValueRect = CGRectMake(, , , );
self.colorLabel = [[UILabel alloc] initWithFrame:colorValueRect];
[self.contentView addSubview:_colorLabel]; }
return self;
} // 重写了Name和Color的set方法,当传递一个新的值时,更新标签的额内容
- (void) setName:(NSString *)n {
if (![n isEqualToString:_name]) {
_name = [n copy];
self.nameLabel.text = _name;
}
} - (void)setColor:(NSString *)c {
if (![c isEqualToString:_color]) {
_color = [c copy];
self.colorLabel.text = _color;
}
} @end
NameAndColorCellTableViewCell.m
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource> @end
ViewController.h
//
// ViewController.m
// Tabel Cells
//
// Created by Jierism on 16/7/20.
// Copyright © 2016年 Jierism. All rights reserved.
// #import "ViewController.h"
#import "NameAndColorCellTableViewCell.h" static NSString *CellTableIdentifier = @"CellTableIdentifier"; @interface ViewController () // 定义一个数组和输出接口
@property (copy,nonatomic) NSArray *computers;
@property (weak,nonatomic) IBOutlet UITableView *tableView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 往数组里定义字典
self.computers = @[@{@"Name" : @"MacBook Air",@"Color" : @"Sliver"},
@{@"Name" : @"MacBook Pro",@"Color" : @"Sliver"},
@{@"Name" : @"iMac",@"Color" : @"Sliver"},
@{@"Name" : @"Mac Mini",@"Color" : @"Sliver"},
@{@"Name" : @"Mac Pro",@"Color" : @"Black"},];
[self.tableView registerClass:[NameAndColorCellTableViewCell class] forCellReuseIdentifier:CellTableIdentifier];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} // DataSource方法 // 返回数组元素个数的行数,这里return的数不能大于元素的个数,否则崩溃
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.computers count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NameAndColorCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier forIndexPath:indexPath]; NSDictionary *rowData = self.computers[indexPath.row]; cell.name = rowData[@"Name"];
cell.color = rowData[@"Color"]; return cell;
} @end
ViewController.m
以上代码手动实现了在表单元中添加了4个Label,并显示相关内容,运行效果如图

这个效果还有另外一种实现方法,就是使用storyboard,用nib实现。不过之前与一位师兄交流中得知,以后工作中用代码实现视图布局比较多,因为会解决很多问题。在这之前自己做的都是使用storyboard,虽然现在觉得使用起来会省事,但是到了开发大型的APP的时候who know,right?所以,自己还需要提升用代码实现布局的能力。
iOS开发-在表单元中添加子视图的更多相关文章
- 关于cell中添加子视图 复用重叠问题的解决方法
问题本质: 因为你要添加的子视图并不是在自定义的cell中实现的,而是根据系统给的UITableViewCell这个类创建的实例,每次进图 cellForRow方法都会创建一个cell,每次都要创 ...
- iOS开发(OC)中的命名规范
开小差:最近发现自己有一个经验主义的毛病,不太容易接受新的知识,这对从事技术研发的人来说不太合理,需要改之. 正文:通过读写大量代码我有自己的一套编程思路和习惯,自认为自己的编码习惯还是不错的,代码结 ...
- IOS开发之表视图(UITableView)
IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于 ...
- iOS开发拓展篇—xib中关于拖拽手势的潜在错误
iOS开发拓展篇—xib中关于拖拽手势的潜在错误 一.错误说明 自定义一个用来封装工具条的类 搭建xib,并添加一个拖拽的手势. 主控制器的代码:加载工具条 封装工具条以及手势拖拽的监听事件 此时运行 ...
- IOS开发效率之为Xcode添加常用的代码片段
IOS开发效率之为Xcode添加常用的代码片段 原文地址:http://blog.csdn.net/pingchangtan367/article/details/30041285 tableview ...
- iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式
iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式 说明: 1)该文简短介绍在iOS开发中遍历字典.数组和集合的几种常见方式. 2)该文对应的代码可以在下面的地址获得:https:// ...
- 解决UIViewController中添加子控制器viewWillAppear不调用问题
问题描述: 我在UICollectionViewController中添加子控制器数组, 并在cellForItem中把子控制器数组中对应的控制器对应的view添加到了UICollectionView ...
- [iOS]在NavigationController中的ScrollView中的子视图都会下移64个像素
情况是这种: 我有一个UINavigationController,设置为self.window的root视图, 然后有一个UIVIewController是UINavigtionController ...
- UIScrollView中添加一个视图,实现让其始终固定在某个位置
ScrollView中添加一个视图,实现让其始终固定在某个位置,如最底部的位置.方法是自定义一个继承UIScrollView,重写它的layoutSubviews方法.代码如下: #import &q ...
随机推荐
- 到底怎么样才叫看书?——Tony Zhao's
到底怎么样才叫看书?——上篇 目录: 一.引入 二.经历了就能理解 三.读书要分级 四.只读经典 五.别吝惜你动笔的那点时间 一.引入 看到这个题目的时候你可能会感到有点好笑:“这还用问,看书就是把书 ...
- XmlDocument类
XmlDocument类是.NET框架的DOC解析器.XmlDocument将XML视为树状结构,它装载XML文档,并在内存中构建该文档的树状结构.下面来看下XmlDocument提供了哪些功能. 一 ...
- sublime text2卸载和重新安装
很多同学使用 sublime text2 的时候,出现一些奇怪的bug,且重启无法修复. 于是,就会想到卸载 sublime text2 再重新安装. 然而,你会发现,重新安装后,这个bug任然存在, ...
- Poj2948Martian Mining(记忆化)
链接 这题意好难懂 看得迷迷糊糊 想的也迷迷糊糊 后来睡了会突然想到了..不就是类似以前的矩阵操作 从右下角记忆化 大的由小的推来 dp[i][j] = max(dp[i-1][j]+s1,dp ...
- 结构体 lock_sys
typedef struct lock_sys_struct lock_sys_t; extern lock_sys_t* lock_sys; struct lock_sys_struct{ hash ...
- bzoj1079: [SCOI2008]着色方案
dp.以上次染色时用的颜色的数量和每种数量所含有的颜色作状态. #include<cstdio> #include<algorithm> #include<cstring ...
- RMI、RPC、SOAP通信技术介绍及比对
1.RMI 使用java的程序员,对于RMI(RemoteMethod Invoke,远程方法调用)一定不陌生,在java中,为了在分布式应用开发时,能够方便调用远程对象,java提供了RMI的API ...
- process thread Fiber(linux)
http://blog.chinaunix.net/uid-21084809-id-2215376.html Processes, kernel threads, user threads, and ...
- 线性存储结构-Stack
Stack继承于Vector,是一个模拟堆栈结构的集合类.当然也属于顺序存储结构.这里注意Android在com.android.layoutlib.bridge.impl包中也有一个Stack的实现 ...
- MFC程序运行流程
->进入入口函数_tWinMain() 程序首先进入文件AppModul.cpp,找到_tWinMain()函数运行,调用其中的AfxWinMain()函数. 由于为了支持UNICODE,C运行 ...