我有一个变量来存储自定义类的对象,该对象已加载到UITableView中。
我给用户一个清除所有内容的选项。运行此命令时,我正在清除存储所有玩家的数组,但是如何清除TableView?
我已经尝试过tableView.reloadData(),但是它什么也没做。
自定义类和全局变量:
var players: [AnyObject] = []
Class Player{
var playerName: String?
init(name:String){
playerName = name
}
}
自定义单元格cellForRowAtIndexPath:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CustomMainCell = tableView.dequeueReusableCellWithIdentifier("CustomMainCell") as CustomMainCell
// Get Player
let thisPlayer:Player = players[indexPath.row] as Player
cell.nameLabel?.text = thisPlayer.playerName
}
return cell
}
设置清除功能:
alertController.addAction(UIAlertAction(title: "Clear Players?", style: .Default, handler: { (action: UIAlertAction!) in
players.removeAll()
self.tableView.reloadData()
}))
在调用之前清除数组tableView.reloadData()
应该起作用。
我假设您numberOfRowsInSection:
也cellForRowAtIndexPath:
正在使用数组数据返回正确的值/对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句