Ios 更改UITableViewCell高度基于UICollectionView内容大小高度
我正在使用Ios 更改UITableViewCell高度基于UICollectionView内容大小高度,ios,uitableview,uicollectionview,row-height,Ios,Uitableview,Uicollectionview,Row Height,我正在使用UICollectionView创建日历 UICollectionView位于UITableViewCell heightForRowAt方法中的UITableView单元格返回UITableView.automaticDimension 用于创建日历的UICollectionView会根据显示的天数更新其高度,例如11月的第一天是星期日,因此将该天置于标签的“星期日”下收集视图必须添加一整行 正常情况下,每个月有5行(周),但当月的第一天发生在周日时,收集视图返回6行 现在,正如我所
UICollectionView
创建日历
UICollectionView
位于UITableViewCell
heightForRowAt
方法中的UITableView单元格
返回UITableView.automaticDimension
用于创建日历的UICollectionView
会根据显示的天数更新其高度,例如11月的第一天是星期日,因此将该天置于标签的“星期日”下收集视图必须添加一整行
正常情况下,每个月有5行(周),但当月的第一天发生在周日时,收集视图
返回6行
现在,正如我所说,我能够根据返回的行数更新UICollectionView的高度,但我无法动态更改包含collectionView
的TableViewCell
的高度
如何根据CollectionView
的高度调整tableViewCell
的大小
编辑 我的手机
我已经为包含
日历视图的单元格的高度设置了UITableView.automaticDimension
在类CalendarView:UIView
中,我创建了一个变量CGFloat calendarH来设置collectionView的默认高度
CollectionView实施
我添加了一个观察者,它可以跟踪collectionview
的高度变化
我目前可以更改集合视图的高度,但其超级视图(bookingCalendarView
)和tableView单元格继续保持固定高度,并且不适应集合视图的高度约束
设置行的日历/月份数据时,确定需要5行还是6行
如果为6,则将高度约束上的.constant
设置为750
如果为5,则将高度约束上的.constant
设置为675
编辑
首先,我建议您忘记使用“自调整大小”集合视图UICollectionView
设计用于根据集合视图的大小布局单元格,在单元格过多时提供自动滚动
试图“自我评估”it可能在一种情况下有效,但在另一种情况下失败。在这种情况下失败的原因是,表格视图在填充集合视图之前,即在它可以“自行调整大小”之前,对单元格进行布局并计算其高度
相反,由于您知道单元格高度为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, 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
// 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, withPad: " ", 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
。。。计算所需高度并在单元格中设置CV高度,或
行高度
。。。计算并返回行所需的高度
旁注:我建议对所有单元格使用自动布局,而不是返回各种行高。如何设置集合视图的高度?@DonMag我没有设置固定高度。。我将顶部、前导、尾随和底部锚定放在cellOK上-集合视图没有固有的高度。如果你知道你的排高是,比如说,20。。。如果有5行,集合视图需要100的高度约束(如果有,加上间距)。。。如果有6行,集合视图需要120的高度限制(如果有,加上间距)。@DonMag我有一个包含6个单元格的集合视图(我已经输入了集合视图达到的最高高度..例如,一周的第一天发生在周日)。高度为450(75(单元格)x 6行),但当collectionView更改行数时,tableviewcell不会更新其大小。我添加了一张xib的照片,以向您展示我输入的约束。注释不用于扩展讨论;这段对话已经结束。