swift学习 - tableView自适应高度2(SnapKit Layout)
SnapKit是Swift中自动布局的框架,相当于Objective-C中的Masonry
下面是tableView自定义cell,使用SnapKit布局的效果图:

详细代码如下:
TYCustomCell.swift
import UIKit
import SnapKit
class TYCustomCell: UITableViewCell {
var imgView: UIImageView?
var titleLab:UILabel?
var despLab:UILabel?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupUI()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI()
}
func setupUI() {
//初始化头像
imgView = UIImageView()
imgView?.image = UIImage.init(named: "img.jpg")
imgView?.layer.borderColor = UIColor.gray.cgColor
imgView?.layer.borderWidth = 1.0
self.addSubview(imgView!)
//顶部的label 初始化
let label1 = UILabel()
label1.font = .systemFont(ofSize: 15)
label1.textColor = .red
self.addSubview(label1)
titleLab = label1
//底部的label 初始化
let label2 = UILabel()
label2.font = .systemFont(ofSize: 14)
label2.textColor = .black
label2.numberOfLines = 0
self.addSubview(label2)
despLab = label2;
//设置布局 SnapKit --- >相当去Objective-C中的Masonry
imgView?.snp.makeConstraints({ (make) in
make.top.left.equalTo(10)
make.width.height.equalTo(40)
})
label1.snp.makeConstraints { (make) in
make.top.equalTo(10)
make.left.equalTo((imgView?.snp.right)!).offset(10)
make.right.equalTo(-10)
make.height.equalTo(21)
}
label2.snp.makeConstraints { (make) in
make.top.equalTo(label1.snp.bottom).offset(10)
make.left.equalTo((imgView?.snp.right)!).offset(10)
make.right.equalTo(-10)
make.bottom.equalTo(-10)
}
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
var tableView:UITableView?;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupTableView()
}
func setupTableView() {
tableView = UITableView(frame: view.bounds, style: .plain)
tableView?.delegate = self;
tableView?.dataSource = self;
tableView?.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView?.register(TYCustomCell.self, forCellReuseIdentifier: "TYCustomCell")
view.addSubview(tableView!)
tableView?.estimatedRowHeight = 44.0
tableView?.rowHeight = UITableViewAutomaticDimension
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TYCustomCell", for: indexPath) as! TYCustomCell
// cell.textLabel?.text = "xxxx\(indexPath.row)"
cell.titleLab?.text = "我是假数据"
if indexPath.row%2==0 {
cell.despLab?.text = "我是假数据我是假数据我是假数据"
}
else
{
if indexPath.row%3 == 0{
cell.despLab?.text = "我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据"
}
else{
cell.despLab?.text = "我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据我是假数据"
}
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
print("第\(indexPath.row)行被点击了")
}
}
重点:swift中tableView如果创建自定义cell,swift中如何使用SnapKit框架进行控件布局
自动计算高度代码:
tableView?.estimatedRowHeight = 44.0
tableView?.rowHeight = UITableViewAutomaticDimension
swift学习 - tableView自适应高度2(SnapKit Layout)的更多相关文章
- swift学习 - tableView自适应高度1(xib autoLayout)
tableView自适应高度 效果图: 源码: class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSo ...
- 纯代码TableView自适应高度(很老的使用方法)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return ...
- IOS Swift语言开发 tableView的重用以及自cell的自适应高度
http://www.aichengxu.com/iOS/11143168.htm 一.准备数据 (这是一个元组,第一个元素为英雄的名字;第二个元素为英雄头像图片的名字,格式为.PNG,如果为其他的格 ...
- TableView cell自适应高度-----xib
1.通过xib创建一个cell,将label进行上左下右,进行适配, self.automaticallyAdjustsScrollViewInsets = NO; self.edgesForExte ...
- [Swift通天遁地]二、表格表单-(12)设置表单文字对齐方式以及自适应高度的文本区域TextArea
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 让tableView的高度等于contentSize的高度、动态调整tableView的高度、tableView的高度自适应布局
文章概要: 1.简介下,tableView中的内容如何高度自适应的布局 2.如何做到让tableView的高度动态调整 还是看图作文吧- 首先,tableView的高度就是用户能够看见里面更大世界的那 ...
- Cell自适应高度及自定义cell混合使…
第一部分:UItableViewCellAdaptionForHeight : cell的自适应高度 第二部分:CustomTableViewCell:自定义cell的混合使用(以简单通讯录为例) = ...
- iOS 【终极方案】精准获取webView内容高度,自适应高度
前言:是这样的,刚写完上一篇文章还没缓过神来,上一篇文章我还提到了,想和大家聊聊原生+H5如何无缝连接的故事.结果我朋友就给我发了两篇他的作品.他的做法也都有独到之处.好的文章都是这样,让你每次看都能 ...
- 怎样让自定义Cell的图片和文本自适应高度
Let's do it! 首先创建一个Model类 包括一个图片名称属性 还有文字内容属性 #import <Foundation/Foundation.h> @interface Mod ...
随机推荐
- 老李分享:robotium常用API 2
断言: 具体请查看官网 断言方法assert(robotium特有的断言方式,实际项目中和Junit的assert方法配合使用) void assertCurrentActivity (String ...
- 老李推荐:第6章7节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览-注入按键事件实例
老李推荐:第6章7节<MonkeyRunner源码剖析>Monkey原理分析-事件源-事件源概览-注入按键事件实例 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜 ...
- CSS3选择器~一看吓一跳,这么多不会
复习CSS时发现很多选择器不会,因为平时很少用到.现在干脆一不做二不修,全部温习一遍.本文参考http://css.doyoe.com/. 一.元素选择器 图片来自:http://css.doyoe. ...
- 转接口IC ADV7280/ADV7280-M:CVBS转MIPI转接口芯片 10位、4倍过采样标清电视视频解码器,支持去隔行
概述ADV7280/ADV7280-M是功能丰富的单芯片.多格式视频解码器.ADV7280/ADV7280-M可自动检测标准模拟基带视频信号,兼容复合.S视频和分量视频形式的NTSC.PAL和SECA ...
- HNOI2017前被虐记及感悟
本文所记录的时间以HNOI2017第一天考试时间为DAY1,前一天为DAY0,以此类推. 本文记载了博主从HNOI2017开始前一周进行全真模拟考试的被虐过程和结果.文章内可能包含博主的不良情绪,如果 ...
- JavaEE开发使用Maven管理的SpringMVC工程
前几篇博客已经陆陆续续的聊了一些Spring的东西,今天博客我们就来聊一下SpringMVC.SpringMVC目前在JavaEE开发中可谓占据一席之地,用起来也是比较顺手的.低耦合,高内聚,利用一些 ...
- MySQL关于check约束无效的解决办法
首先看下面这段MySQL的操作,我新建了一个含有a和b的表,其中a用check约束必须大于0,然而我插入了一条(-2,1,1)的数据,其中a=-2,也是成功插入的. 所以MySQL只是check,但是 ...
- 本地yum服务搭建
1.准备linux ISO系统镜像文件 (例如:rhel-server-5.5-i386-dvd.iso) 2.linux虚拟机(centos 7 192.168.50.24 ),启动sshd服务 ...
- 【Spark2.0源码学习】-4.Master启动
Master作为Endpoint的具体实例,下面我们介绍一下Master启动以及OnStart指令后的相关工作 一.脚本概览 下面是一个举例: /opt/jdk1..0_79/ ...
- 虚拟机下安装ubuntu系统
前期准备工具; 1,Oracle VM VirtualBox虚拟机 下载地址:http://pan.baidu.com/s/1miSaGvm 密码:c3dy 2,ubuntu系统文件 下载地址:htt ...