xcode 在 Swift 中刷新 UITableView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35552083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh UITableView in Swift
提问by Jojo
I have a problem with refreshing my custom UITableView inside an UIViewController.
我在 UIViewController 中刷新自定义 UITableView 时遇到问题。
When appear the tableView has all its cell with a clear backgroundcolor. I have a "start" button above and when I click on it I want to have all the cell in another color.
当出现时,tableView 的所有单元格都具有清晰的背景色。我上面有一个“开始”按钮,当我点击它时,我想让所有单元格都变成另一种颜色。
I have specified the rules in:
我已经指定了规则:
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if self.status == "start" {
if indexPath != self.currentIndexPath {
cell.backgroundColor = UIColor(red: 0 , green: 0, blue: 0, alpha: 0.5)
} else {
cell.backgroundColor = UIColor.clearColor()
}
} else {
cell.backgroundColor = UIColor.clearColor()
}
}
In the "start" action button, I call: self.tableView.reloadData
在“开始”操作按钮中,我调用: self.tableView.reloadData
@IBAction func startAction(sender: AnyObject) {
self.currentIndexPath = NSIndexPath(forRow: 0, inSection: 0)
self.status = "start"
self.tableView.reloadData()
}
But it's not working very well as I must scroll to update the background color.
但效果不佳,因为我必须滚动以更新背景颜色。
I tried to use the self.tableView.reloadRowsAtIndexPaths
method. But the result is the same.
我尝试使用该self.tableView.reloadRowsAtIndexPaths
方法。但结果是一样的。
I always must scroll the tableView to update the background color or some images.
我总是必须滚动 tableView 来更新背景颜色或一些图像。
Where am I wrong ?
我哪里错了?
回答by Richard Fairhurst
Replace your call to reloadData with:
将您对 reloadData 的调用替换为:
DispatchQueue.main.async { self.tableView.reloadData() }
回答by Wez
You should probably put your logic inside the cellForRowAtIndexPath
delegate method, this will get called when you reload the table.
您可能应该将您的逻辑放在cellForRowAtIndexPath
委托方法中,这将在您重新加载表时调用。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath)
if self.status == "start" {
if indexPath != self.currentIndexPath {
cell.backgroundColor = UIColor(red: 0 , green: 0, blue: 0, alpha: 0.5)
} else {
cell.backgroundColor = UIColor.clearColor()
}
} else {
cell.backgroundColor = UIColor.clearColor()
}
return cell
}
As I can't see your current implementation of this method I have just guessed at your dequeuing of the cell, you may need to change this slightly, If you can post this code in your question I can help.
由于我看不到您当前对该方法的实现,我只是猜测您的单元格出列,您可能需要稍微更改一下,如果您可以在问题中发布此代码,我可以提供帮助。
回答by Peter Foti
You've got the logic in the wrong place. willDisplayCell
is called just before the cell is drawn which is why it makes sense that you're seeing the change when you scroll. Calling reloadData
is going to call cellForRowAtIndexPath
so you should move your logic to that method.
你在错误的地方找到了逻辑。willDisplayCell
在绘制单元格之前调用,这就是为什么滚动时看到更改是有意义的。调用reloadData
将调用,cellForRowAtIndexPath
因此您应该将逻辑移至该方法。
回答by Reshmi Majumder
Instead of adding your codes on WillDisplayCell add in cellForRowAtIndexPath
不要在 WillDisplayCell 上添加代码,而是在 cellForRowAtIndexPath 中添加
@IBAction func startAction(sender: UIButton)
{
let buttonPosition : CGPoint = sender.convertPoint(CGPointZero, toView: self.tableview )
self.currentIndexPath = self.tableview.indexPathForRowAtPoint(buttonPosition)!
self.status = "start"
self.tableView.reloadData()
}