ios - UITableView 卡住单元格。如何解决?

标签 ios swift uitableview

我有 tableView cellForRowAtIndexPath 功能:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let cell: ProjectCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! ProjectCell
  let rightUtilityButtons = NSMutableArray()
  rightUtilityButtons.sw_addUtilityButtonWithColor(UIColor(red: 255.0/255.0, green: 200.0/255.0, blue: 122.0/255.0, alpha: 1.0), icon: UIImage(named: "ic_edit"))
  rightUtilityButtons.sw_addUtilityButtonWithColor(UIColor(red: 255.0/255.0, green: 120.0/255.0, blue: 122.0/255.0, alpha: 1.0), icon: UIImage(named: "ic_close"))
  cell.setRightUtilityButtons(rightUtilityButtons as [AnyObject], withButtonWidth: 70)
  cell.delegate = self

  let project = projects[indexPath.row] as! Project
  cell.projectNameLabel.text = project.name
  cell.projectDescriptionLabel.text = project.desc
  cell.projectDateLabel.text = project.duedate 

  cell.customView.backgroundColor = getColor(indexPath.row * 10)
  cell.backView.layer.cornerRadius = 6
  cell.backView.layer.shadowColor = UIColor.blackColor().CGColor
  cell.backView.layer.shadowOffset = CGSize(width: -1, height: 1)
  cell.backView.layer.shadowOpacity = 0.5   

  return cell
}

问题在这里:

cell.backView.layer.shadowColor = UIColor.blackColor().CGColor
cell.backView.layer.shadowOffset = CGSize(width: -1, height: 1)
cell.backView.layer.shadowOpacity = 0.5

如果没有这段代码,我的 TableView 会很好用。如何解决这个问题呢。也许我的单元格 View 更好的地方是 willDisplayCell,但是如何将这段代码放在那里?

最佳答案

  1. 将设置阴影的代码移动到单元格的初始化。现在,每次委托(delegate)将单元格返回到 TableView 时都会设置阴影。对于刚刚被重复使用并且已经配置了阴影的单元格,您不必执行此操作。

  2. 记得在处理阴影时设置shadowPath。它提高了性能。

    [view.layer setShadowPath:[[UIBezierPath  bezierPathWithRect:view.bounds] CGPath]];
    

关于ios - UITableView 卡住单元格。如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33609544/

相关文章:

iOS 实例变量获取 "NULLED"未知原因

iOS - 方法(在另一个类中)执行了两次

ios - 在 SKSpriteNode 上叠加两个纹理

ios - 圆角半径不适合动态分组的 tableView swift

iphone - iOS 5 UITableViewCell 自定义 XIB 又名 "Am I losing my mind?"

ios - 在 iOS 上编译 Phonegap 项目

ios - Swift:MapKit 自定义图像标注

swift - CKBrowserSwitcherViewController 类覆盖了 -traitCollection getter... 这个消息是什么意思?

ios - 如何使用 YouTube API 获取 channel 名称?

swift - 有没有更干净的方法来部分填充 tableView