自定义扩展 UITableViewCell 有问题?
Custom expanding UITableViewCell has a glitch?
这是 glitch。正如您所看到的,当我将自定义 UITableViewCell SummaryCell
推出应用程序的视图时,单元格出现故障并且没有将 UILabels
dayOfTheWeek
和 totalAmountSpent
放在适当的位置.但是如果你点击有问题的单元格,它 returns 就正常了。
下面是我用来创建自定义单元格summaryCell
的UITableViewController SummaryTableViewController
的一些方法:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Create day of week cell
let cell = tableView.dequeueReusableCellWithIdentifier("summaryCell", forIndexPath: indexPath) as! SummaryCell
let day = dayOfWeek[indexPath.row]
let height = CGFloat(55)
let dayOfWeekWidth = CGFloat(80)
cell.dayOfWeek.text = day.rawValue.uppercaseString
cell.dayOfWeek.frame = CGRectMake(0, 0, dayOfWeekWidth, height)
cell.dayOfWeek.backgroundColor = colorOfDay[day]
cell.totalAmountSpent.text = "$\(totalSpentPerDay[indexPath.row])"
cell.totalAmountSpent.frame = CGRectMake(cell.dayOfWeek.frame.maxX + 1, 0, view.bounds.width - dayOfWeekWidth, height)
cell.totalAmountSpent.backgroundColor = colorOfDay[day]
cell.totalAmountSpentView.backgroundColor = colorOfDay[day]
cell.heightOfMainView.constant = normalCellHeight
//^I have this to make sure the height of the cell is the same as
//the height of the mainView in the nib file `summaryCell` so that
//when the table loads intially, only the mainView of the `summaryCell` shows up.
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if expandedCellPaths.contains(indexPath){
let index = expandedCellPaths.indexOf(indexPath)
expandedCellPaths.removeAtIndex(index!)
}else{
expandedCellPaths.append(indexPath)
}
//old trick to animate cell expand/collapse
tableView.beginUpdates()
tableView.endUpdates()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let expandedCellHeight = normalCellHeight*2
if expandedCellPaths.contains(indexPath){
return expandedCellHeight
}else{
return normalCellHeight
}
}
这是the nib file & its constraints对应我的习惯UITableViewCell SummaryCell
关于为什么会发生这种情况,我的 2 个最佳猜测是 (1) 我的 nib 文件中的约束 summaryCell
或 (2) 我的行 UITableViewConroller SummaryTableViewController
:
如果有人能告诉我如何解决这个问题,我们将不胜感激!
tableView.beginUpdates()
tableView.endUpdates()
其实没关系!我意识到我的错误!我只需要删除 tableView(:cellForRowAtIndexPath:)
:
中的这些行
let height = CGFloat(55)
let dayOfWeekWidth = CGFloat(80)
cell.dayOfWeek.frame = CGRectMake(0, 0, dayOfWeekWidth, height)
cell.totalAmountSpent.frame = CGRectMake(cell.dayOfWeek.frame.maxX + 1, 0, view.bounds.width - dayOfWeekWidth, height)
这是 glitch。正如您所看到的,当我将自定义 UITableViewCell SummaryCell
推出应用程序的视图时,单元格出现故障并且没有将 UILabels
dayOfTheWeek
和 totalAmountSpent
放在适当的位置.但是如果你点击有问题的单元格,它 returns 就正常了。
下面是我用来创建自定义单元格summaryCell
的UITableViewController SummaryTableViewController
的一些方法:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Create day of week cell
let cell = tableView.dequeueReusableCellWithIdentifier("summaryCell", forIndexPath: indexPath) as! SummaryCell
let day = dayOfWeek[indexPath.row]
let height = CGFloat(55)
let dayOfWeekWidth = CGFloat(80)
cell.dayOfWeek.text = day.rawValue.uppercaseString
cell.dayOfWeek.frame = CGRectMake(0, 0, dayOfWeekWidth, height)
cell.dayOfWeek.backgroundColor = colorOfDay[day]
cell.totalAmountSpent.text = "$\(totalSpentPerDay[indexPath.row])"
cell.totalAmountSpent.frame = CGRectMake(cell.dayOfWeek.frame.maxX + 1, 0, view.bounds.width - dayOfWeekWidth, height)
cell.totalAmountSpent.backgroundColor = colorOfDay[day]
cell.totalAmountSpentView.backgroundColor = colorOfDay[day]
cell.heightOfMainView.constant = normalCellHeight
//^I have this to make sure the height of the cell is the same as
//the height of the mainView in the nib file `summaryCell` so that
//when the table loads intially, only the mainView of the `summaryCell` shows up.
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if expandedCellPaths.contains(indexPath){
let index = expandedCellPaths.indexOf(indexPath)
expandedCellPaths.removeAtIndex(index!)
}else{
expandedCellPaths.append(indexPath)
}
//old trick to animate cell expand/collapse
tableView.beginUpdates()
tableView.endUpdates()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let expandedCellHeight = normalCellHeight*2
if expandedCellPaths.contains(indexPath){
return expandedCellHeight
}else{
return normalCellHeight
}
}
这是the nib file & its constraints对应我的习惯UITableViewCell SummaryCell
关于为什么会发生这种情况,我的 2 个最佳猜测是 (1) 我的 nib 文件中的约束 summaryCell
或 (2) 我的行 UITableViewConroller SummaryTableViewController
:
如果有人能告诉我如何解决这个问题,我们将不胜感激! tableView.beginUpdates() tableView.endUpdates()
其实没关系!我意识到我的错误!我只需要删除 tableView(:cellForRowAtIndexPath:)
:
let height = CGFloat(55)
let dayOfWeekWidth = CGFloat(80)
cell.dayOfWeek.frame = CGRectMake(0, 0, dayOfWeekWidth, height)
cell.totalAmountSpent.frame = CGRectMake(cell.dayOfWeek.frame.maxX + 1, 0, view.bounds.width - dayOfWeekWidth, height)