【发布时间】:2018-12-30 15:59:56
【问题描述】:
我正在制作一个简单的表格视图。
我面临的问题是,当我向下和向上滚动时,单元格上方出现了另一个单元格。
请看下面的 2 张图片。
在这里您看到单元格出现在顶部,并且计数器不是第一个单元格应为的零。您还可以看到颜色较深。
代码如下:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) //as! ArrangementCell
let card = CardHighlight(frame: CGRect(x: cell.bounds.origin.x + 30, y: cell.bounds.origin.y + 10, width: cell.bounds.width - 60 , height: (cell.bounds.width + 40)/2.6282051282))
card.backgroundColor = UIColor(red: 0, green: 94/255, blue: 112/255, alpha: 0.1)
card.title = String(Counter)
card.hasParallax = true
card.shadowOpacity = 0.6
card.shadowBlur = 7
card.cardRadius = 7
Counter = Counter + 1
let cardContentVC = storyboard!.instantiateViewController(withIdentifier: "CardContent") as! DetaljertArrangement
card.shouldPresent(cardContentVC, from: self, fullscreen: false)
cell.contentView.addSubview(card)
return cell
}
我怎样才能避免这个问题?
【问题讨论】:
-
单元被重复使用,您正在添加 always 一个新的子视图.... 使用 custom 单元并在界面中添加视图(一次)生成器。
标签: ios swift uitableview tableview