如何解决更改基于UITableViewCell Height的UICollectionView内容大小的高度
我正在使用UICollectionView
创建的日历。
UICollectionView
位于UITableViewCell
内部
UITableViewCell
方法heightForRowAt
return UITableView.automaticDimension
用于创建日历的UICollectionView
会根据显示的天数来更新其高度,例如 11月的月份的第一天是星期日,因此要将日期置于标签“星期日” 下,collectionView
必须添加整行。
通常每个月有5行(周),但是当每月的第一天发生在星期日时,collectionview
返回6
现在,正如我所说,我能够根据返回的行数来更新 UICollectionView 的高度,但是我无法动态更改{{1的 height }},其中包含TableViewCell
如何根据其中collectionView
的高度调整tableViewCell
的大小?
编辑
我的手机
我为包含CollectionView
的单元格的高度设置了UITableView.automaticDimension
![enter image description here](https://i.stack.imgur.com/VNzZO.png)
在类CalendarView
中,我创建了一个变量CGFloat calendarH来设置collectionView的默认高度
CollectionView实施
我添加了一个观察者,它在CalendarView: UIView
发生变化时跟踪其高度
我目前可以更改collctionView
的高度,但是其超级视图(collectionView
)和tableView单元格将继续保持固定的高度,并且无法适应bookingCalendarView
解决方法
您需要为集合视图的高度约束创建一个@IBOutlet
。
设置行的日历/月份数据时,确定需要5行还是6行。
如果为6,则将高度约束上的.constant
设置为750
如果为5,则将高度约束上的.constant
设置为675
修改
首先,我建议您不要使用“自动调整大小”集合视图。 UICollectionView
旨在根据集合视图的大小来布置单元格 ,当单元格过多时可以自动滚动。
尝试“自我调整”大小 可能 在一个实例中有效,但在另一个实例中无效。在这种情况下失败的原因是因为您的表格视图布局了单元格,并在填充视图 之前计算了其高度,然后才可以“自行调整大小” 。”
相反,由于您知道单元格的高度为75,因此您可以计算日历需要多少行,并为集合视图的高度限制设置.constant
,或者(因为您已经在使用heightForRowAt
)计算那里的行高。
看下面的代码:
let dateComponents = DateComponents(year: year,month: month)
// startDate will be the first date of the month (Jan 1,Feb 1,Mar 1,etc...)
guard let startDate = calendar.date(from: dateComponents) else {
fatalError("Something is wrong with the date!")
}
// get the range of days in the month
guard let range = calendar.range(of: .day,in: .month,for: startDate) else {
fatalError("Something is wrong with the date!")
}
// get number of days in the month
let numberOfDaysInMonth = range.count
// get the day of the week for the first date in the month
// this returns 1-based numbering
// Nov 1,2020 was a Sunday,so this would return 1
let startDayOfWeek = Calendar.current.component(.weekday,from: startDate)
// add the "leading days to the start date"
// so,if startDayOfWeek == 3 (Tuesday)
// we need to add 2 "empty day cells" for Sunday and Monday
let totalCellsNeeded = numberOfDaysInMonth + (startDayOfWeek - 1)
// calculate number of rows needed -- this will be 4,5 or 6
// the only time we get 4 is if Feb 1st in a non-leapYear falls on a Sunday
let numRows = Int(ceil(Double(totalCellsNeeded) / Double(7)))
// we now know the Height needed for the collection view
// you said your calendar cell height is 75,so...
// cvHeight = numRows * 75
我们可以将其循环放置,然后print()
将信息override func viewDidLoad() {
super.viewDidLoad()
let calendar = Calendar.current
// 2026 is the next year where Feb starts on a Sunday
// so let's use that year to see that we get 4 rows for Feb
let year = 2026
for month in 1...12 {
let dateComponents = DateComponents(year: year,month: month)
// startDate will be the first date of the month (Jan 1,etc...)
guard let startDate = calendar.date(from: dateComponents) else {
fatalError("Something is wrong with the date!")
}
// get the range of days in the month
guard let range = calendar.range(of: .day,for: startDate) else {
fatalError("Something is wrong with the date!")
}
// get number of days in the month
let numberOfDaysInMonth = range.count
// get the day of the week for the first date in the month
// this returns 1-based numbering
// Nov 1,so this would return 1
let startDayOfWeek = Calendar.current.component(.weekday,from: startDate)
// add the "leading days to the start date"
// so,if startDayOfWeek == 3 (Tuesday)
// we need to add 2 "empty day cells" for Sunday and Monday
let totalCellsNeeded = numberOfDaysInMonth + (startDayOfWeek - 1)
// calculate number of rows needed -- this will be 4,5 or 6
// the only time we get 4 is if Feb 1st in a non-leapYear falls on a Sunday
let numRows = Int(ceil(Double(totalCellsNeeded) / Double(7)))
// we now know the Height needed for the collection view
// you said your calendar cell height is 75,so...
// cvHeight = numRows * 75
// debug output
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
let dayName = dateFormatter.string(from: startDate)
dateFormatter.dateFormat = "LLLL y"
let dateString = dateFormatter.string(from: startDate)
let dayPadded = dayName.padding(toLength: 10,withPad: " ",startingAt: 0)
let datePadded = dateString.padding(toLength: 16,startingAt: 0)
print("\(datePadded) has \(numberOfDaysInMonth) days,starting on \(dayPadded) requiring \(numRows) rows")
}
}
到调试控制台,如下所示:
January 2026 has 31 days,starting on Thursday requiring 5 rows
February 2026 has 28 days,starting on Sunday requiring 4 rows
March 2026 has 31 days,starting on Sunday requiring 5 rows
April 2026 has 30 days,starting on Wednesday requiring 5 rows
May 2026 has 31 days,starting on Friday requiring 6 rows
June 2026 has 30 days,starting on Monday requiring 5 rows
July 2026 has 31 days,starting on Wednesday requiring 5 rows
August 2026 has 31 days,starting on Saturday requiring 6 rows
September 2026 has 30 days,starting on Tuesday requiring 5 rows
October 2026 has 31 days,starting on Thursday requiring 5 rows
November 2026 has 30 days,starting on Sunday requiring 5 rows
December 2026 has 31 days,starting on Tuesday requiring 5 rows
这是输出:
cellForRowAt
所以...要么在
-
heightForRowAt
...计算所需的高度并在单元格中设置CV高度,或者 -
[[apim.throttling.url_group]] traffic_manager_urls = ["tcp://Traffic-Manager-host:9611"] traffic_manager_auth_urls = ["ssl://Traffic-Manager-host:9711"] [apim.throttling] service_url = "https://Traffic-Manager-host:${mgt.transport.https.port}/services/" throttle_decision_endpoints = ["tcp://Traffic-Manager-host:5672"]
...计算并返回该行所需的高度
侧面说明:建议您对所有单元格使用自动布局,而不要返回各种行高。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。