自定义扩展UITableViewCell有一个小故障?

时间:2016-01-09 07:31:01

标签: ios swift uitableview custom-cell

这是glitch。正如您所看到的那样,当我将自定义UITableViewCell SummaryCell推出应用视图时,单元格会出现故障并且不会将UILabels dayOfTheWeektotalAmountSpent放在适当的位置。但如果你点击毛刺细胞,它会恢复正常。

以下是我用于创建自定义单元格UITableViewController SummaryTableViewController的{​​{1}}的一些方法:

summaryCell

这是the nib file & its constraints,对应于我的自定义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 } }

我最好猜测为什么会发生这种情况(1)我的nib文件UITableViewCell SummaryCell(2)中的约束summaryCell

如果有人能告诉我如何解决这个故障,我们将不胜感激!     tableView.beginUpdates()     tableView.endUpdates()

1 个答案:

答案 0 :(得分:0)

其实没关系!我意识到自己的错误!我只需要在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)