swift 自定义cell -----------------------简单学习--------第一篇
来源:互联网 发布:李嘉诚撤资 知乎 编辑:程序博客网 时间:2024/06/05 05:03
1.自定义cell
//// FirstSwiftCell.swift// SwiftDeKeYiCai////import UIKitclass FirstSwiftCell: UITableViewCell { var titleLabel:UILabel? var picImageVIew:UIImageView? override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) self.setUpUI() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } func setUpUI() { self.titleLabel = UILabel.init() self.titleLabel?.backgroundColor = UIColor.clear; self.titleLabel?.frame = CGRect(x:65, y:0, width:100, height:50) self.titleLabel?.textColor = UIColor.magenta self.titleLabel?.text = "第一条显示的数据" self.titleLabel?.font = UIFont.systemFont(ofSize: 15) self.titleLabel?.textAlignment = NSTextAlignment.left; self.contentView.addSubview(self.titleLabel!) self.picImageVIew = UIImageView() self.picImageVIew?.frame = CGRect(x:15, y:5, width:40, height:40) self.picImageVIew?.image = UIImage.init(named: "1.png") self.contentView.addSubview(self.picImageVIew!) } }
2.cell添加tableview --创建tableVIew。viewController 代码
//// ViewController.swift// SwiftDeKeYiCai////import Foundationimport UIKitclass ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { var tableView:UITableView! var array = ["111", "222", "333", "444"] override func viewDidLoad() { super.viewDidLoad() let rect = self.view.frame tableView = UITableView(frame:rect) tableView.delegate = self tableView.dataSource = self let view = UIView() tableView.tableFooterView = view self.view.addSubview(tableView) tableView.register(FirstSwiftCell.self, forCellReuseIdentifier: "cellid") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func numberOfSections(in tableView: UITableView) -> Int { return 1; } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.count; } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 50; } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellid="cellid" var cell:FirstSwiftCell = tableView.dequeueReusableCell(withIdentifier: cellid) as! FirstSwiftCell if cell.isEqual(nil) { cell=FirstSwiftCell(style: UITableViewCellStyle.default, reuseIdentifier: cellid) } cell.selectionStyle = UITableViewCellSelectionStyle.none cell.titleLabel?.text = array[indexPath.row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { }}
4.简单的显示效果
阅读全文
0 0
- swift 自定义cell -----------------------简单学习--------第一篇
- Swift中自定义Cell
- Swift练习小demo tableView 自定义cell 简单实用
- [Swift 学习] 第一篇:基本语法
- Swift UITableView使用自定义cell
- swift自定义cell的坑
- IOS swift 自定义Tableview cell
- Swift 3学习笔记第一篇(语法概览)
- IOS学习 Cell自定义
- swift UICollectionView 代码创建 和自定义Cell
- iOS8自定义Collection View Cell - Swift教程
- Swift TableView自定义Cell的高度
- iOS swift 自定义tableView不显示cell
- 第一篇---简单介绍
- 简单servlet第一篇
- Python学习第一篇:python的简单使用
- 从零开始学swift 第一篇 环境安装
- XZ_Swift之Swift基础第一篇
- cookie和session详解
- CF295B Greg and Graph(离线+Floyd)
- javabean的使用原则
- centos mongodb 3.4 认证配置
- 调整Linux系统时区
- swift 自定义cell -----------------------简单学习--------第一篇
- IDEA文件编码修改
- 对象,方法的调用
- 自媒体时代的5种淘客模式分析
- 98DX3236初始化脚本程序
- 求一个数组中奇数的平均数
- KEIL C258 _mspace illegal in struct/union
- 责任链模式
- Spring中用到的9种设计模式