1
2
3
4
|
//设置estimatedRowHeight属性默认值
self
.tableView!.estimatedRowHeight = 44.0
//rowHeight属性设置为UITableViewAutomaticDimension
self
.tableView!.rowHeight =
UITableViewAutomaticDimension
|
1
2
3
4
5
6
7
8
9
10
11
|
import
UIKit
class
MyCollectionViewCell
:
UICollectionViewCell
{
//用于显示封面缩略图
@IBOutlet
weak
var
imageView:
UIImageView
!
override
func
awakeFromNib() {
super
.awakeFromNib()
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
import
UIKit
class
MyTableViewCell
:
UITableViewCell
,
UICollectionViewDelegate
,
UICollectionViewDataSource
{
//单元格标题
@IBOutlet
weak
var
titleLabel:
UILabel
!
//封面图片集合列表
@IBOutlet
weak
var
collectionView:
UICollectionView
!
//collectionView的高度约束
@IBOutlet
weak
var
collectionViewHeight:
NSLayoutConstraint
!
//封面数据
var
images:[
String
] = []
override
func
awakeFromNib() {
super
.awakeFromNib()
//设置collectionView的代理
self
.collectionView.delegate =
self
self
.collectionView.dataSource =
self
// 注册CollectionViewCell
self
.collectionView!.register(
UINib
(nibName:
"MyCollectionViewCell"
, bundle:
nil
),
forCellWithReuseIdentifier:
"myCell"
)
}
//加载数据
func
reloadData(title:
String
, images:[
String
]) {
//设置标题
self
.titleLabel.text = title
//保存图片数据
self
.images = images
//collectionView重新加载数据
self
.collectionView.reloadData()
//更新collectionView的高度约束
let
contentSize =
self
.collectionView.collectionViewLayout.collectionViewContentSize
collectionViewHeight.constant = contentSize.height
}
//返回collectionView的单元格数量
func
collectionView(_ collectionView:
UICollectionView
,
numberOfItemsInSection section:
Int
) ->
Int
{
return
images.count
}
//返回对应的单元格
func
collectionView(_ collectionView:
UICollectionView
,
cellForItemAt indexPath:
IndexPath
) ->
UICollectionViewCell
{
let
cell = collectionView.dequeueReusableCell(withReuseIdentifier:
"myCell"
,
for
: indexPath)
as
!
MyCollectionViewCell
cell.imageView.image =
UIImage
(named: images[indexPath.item])
return
cell
}
//绘制单元格底部横线
override
func
draw(_ rect:
CGRect
) {
//线宽
let
lineWidth = 1 /
UIScreen
.main.scale
//线偏移量
let
lineAdjustOffset = 1 /
UIScreen
.main.scale / 2
//线条颜色
let
lineColor =
UIColor
(red: 0xe0/255, green: 0xe0/255, blue: 0xe0/255, alpha: 1)
//获取绘图上下文
guard
let
context =
UIGraphicsGetCurrentContext
()
else
{
return
}
//创建一个矩形,它的所有边都内缩固定的偏移量
let
drawingRect =
self
.bounds.insetBy(dx: lineAdjustOffset, dy: lineAdjustOffset)
//创建并设置路径
let
path =
CGMutablePath
()
path.move(to:
CGPoint
(x: drawingRect.minX, y: drawingRect.maxY))
path.addLine(to:
CGPoint
(x: drawingRect.maxX, y: drawingRect.maxY))
//添加路径到图形上下文
context.addPath(path)
//设置笔触颜色
context.setStrokeColor(lineColor.cgColor)
//设置笔触宽度
context.setLineWidth(lineWidth)
//绘制路径
context.strokePath()
}
override
func
setSelected(_ selected:
Bool
, animated:
Bool
) {
super
.setSelected(selected, animated: animated)
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
import
UIKit
//每月书籍
struct
BookPreview
{
var
title:
String
var
images:[
String
]
}
class
ViewController
:
UIViewController
,
UITableViewDelegate
,
UITableViewDataSource
{
//所有书籍数据
let
books = [
BookPreview
(title:
"五月新书"
, images: [
"0.jpg"
,
"1.jpg"
,
"2.jpg"
,
"3.jpg"
,
"4.jpg"
,
"5.jpg"
,
"6.jpg"
]),
BookPreview
(title:
"六月新书"
, images: [
"7.jpg"
,
"8.jpg"
,
"9.jpg"
]),
BookPreview
(title:
"七月新书"
, images: [
"10.jpg"
,
"11.jpg"
,
"12.jpg"
,
"13.jpg"
])
]
//显示内容的tableView
@IBOutlet
weak
var
tableView:
UITableView
!
override
func
loadView() {
super
.loadView()
}
override
func
viewDidLoad() {
super
.viewDidLoad()
//设置tableView代理
self
.tableView!.delegate =
self
self
.tableView!.dataSource =
self
//去除单元格分隔线
self
.tableView!.separatorStyle = .none
//创建一个重用的单元格
self
.tableView!.register(
UINib
(nibName:
"MyTableViewCell"
, bundle:
nil
),
forCellReuseIdentifier:
"myCell"
)
//设置estimatedRowHeight属性默认值
self
.tableView!.estimatedRowHeight = 44.0
//rowHeight属性设置为UITableViewAutomaticDimension
self
.tableView!.rowHeight =
UITableViewAutomaticDimension
}
//在本例中,只有一个分区
func
numberOfSectionsInTableView(tableView:
UITableView
) ->
Int
{
return
1;
}
//返回表格行数
func
tableView(_ tableView:
UITableView
, numberOfRowsInSection section:
Int
) ->
Int
{
return
self
.books.count
}
//创建各单元显示内容(创建参数indexPath指定的单元)
func
tableView(_ tableView:
UITableView
, cellForRowAt indexPath:
IndexPath
)
->
UITableViewCell
{
let
cell = tableView.dequeueReusableCell(withIdentifier:
"myCell"
)
as
!
MyTableViewCell
//下面这两个语句一定要添加,否则第一屏显示的collection view尺寸,以及里面的单元格位置会不正确
cell.frame = tableView.bounds
cell.layoutIfNeeded()
//重新加载单元格数据
cell.reloadData(title:books[indexPath.row].title,
images: books[indexPath.row].images)
return
cell
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
|