IOS开发单元格自定义方法之一
2015-07-27 17:01
309 查看
第一种方法是直接写一个继承UITableViewCell。然后动态的添加子视图方式
具体步骤:
1.新建立一个cell类,继承UITableViewCell
2.在这个类里面,定义属性,比如UILabel
3.重载构造函数,把子视图添加上
4.可以使用这个类了。
CityCellTableViewCell类:
viewController重载tableView方法:
具体步骤:
1.新建立一个cell类,继承UITableViewCell
2.在这个类里面,定义属性,比如UILabel
3.重载构造函数,把子视图添加上
4.可以使用这个类了。
CityCellTableViewCell类:
// // CityCellTableViewCell.swift // UITableViewDemo0 // // Created by 王丰 on 7/27/15. // Copyright (c) 2015 wangfeng. All rights reserved. // import UIKit class CityCellTableViewCell: UITableViewCell { var cityLabel:UILabel? var cityTextFiled:UITextField? var citySwitch:UISwitch? override init(style: UITableViewCellStyle,reuseIdentifier: String?){ super.init(style: style, reuseIdentifier: reuseIdentifier) //初始化子视图,子控件,然后添加到当前视图 cityLabel = UILabel(frame: CGRect(x: 5, y: 5, width: 80, height: 40)) cityTextFiled = UITextField(frame: CGRect(x: 90, y: 5, width: 80, height: 40)) citySwitch = UISwitch(frame: CGRect(x: 200, y: 5, width: 80, height: 40)) //添加到当前视图 self.addSubview(cityLabel!) self.addSubview(cityTextFiled!) self.addSubview(citySwitch!) } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }
viewController重载tableView方法:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ var cellId = "mycell" var cell:CityCellTableViewCell? = (tableView.dequeueReusableCellWithIdentifier(cellId) as? CityCellTableViewCell) if(cell == nil){ cell = CityCellTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellId) } cell?.cityLabel?.text = cities[indexPath.row] cell?.cityTextFiled?.placeholder = "input number" cell?.citySwitch?.on = true return cell! }
相关文章推荐
- IOS手势操作(拖动、捏合、旋转、点按、长按、轻扫、自定义)
- IOS-NSRunLoop
- ios 判断小数点. 后几位 && 小数点后有数据显示数据,没有则不显示数据
- 我的iOS工程结构
- 【iOS开发】---- 把代码上传到github
- ios—项目开发需求文档
- Cacti+Nagios完全攻略(二)整合cacti与nagios安装部署
- Nagios 监控Windows服务器(详细篇)
- nagios 执行check_disk时报错“NRPE: Command 'check_disk' not defined
- Nagios邮件报警设置(解决邮件中文乱码问题)
- iOS的面向对象(类和对象)
- iOS 地理位置反编码
- iOS 8 定位
- iOS- 用MapKit和CoreLocation 来实现移动设备(地图与定位)
- iOS开发动画(Animation)图片360度不停旋转
- IOS开发—NSDateFormatter
- 如何用IOS调用WebService(SOAP接口)
- iOS_nil、Nil、NULL、NSNull的区别
- IOS 设备常亮
- 27个提升效率的iOS开源库推荐