我通过拉下来刷新表格视图。它刷新存储在解析中的歌曲数组。我在应用程序启动时获得了一个版本的数组,但是当我向数组添加内容然后刷新它时,程序再次添加所有内容以及新内容。所以现在除了新项目之外我还有两次重复。我该如何避免重复?
override func viewDidLoad() {
super.viewDidLoad()
getSongs()
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
self.table.addSubview(refreshControl)
// Do any additional setup after loading the view.
}
func getSongs(){
let retrieve = PFQuery(className: "Pointer")
retrieve.findObjectsInBackgroundWithBlock({
(objects: [AnyObject]?, error: NSError?) -> Void in
var object = objects as! [PFObject]
if(error == nil){
for i in 0...object.count-1{
//self.ret.append(object[i].valueForKey("user") as! String)
if (object[i].valueForKey("user")!.objectId == currentUserID){
//print(object[i].valueForKey("title") as! String)
self.ret.append(object[i].valueForKey("title") as! String)
self.ter.append(object[i].valueForKey("artist") as! String)
print(self.ret)
print(self.ter)
}
}
}
})
}
func refresh(sender: AnyObject){
getSongs()
table.reloadData()
self.refreshControl.endRefreshing()
}
答案 0 :(得分:2)
@Tim是对的。您必须先刷新数据。
func getSongs(){
let retrieve = PFQuery(className: "Pointer")
retrieve.findObjectsInBackgroundWithBlock({
(objects: [AnyObject]?, error: NSError?) -> Void in
var object = objects as! [PFObject]
if(error == nil){
// refresh data
self.ret = []
self.ter = []
for i in 0...object.count-1{
//self.ret.append(object[i].valueForKey("user") as! String)
if (object[i].valueForKey("user")!.objectId == currentUserID){
//print(object[i].valueForKey("title") as! String)
self.ret.append(object[i].valueForKey("title") as! String)
self.ter.append(object[i].valueForKey("artist") as! String)
print(self.ret)
print(self.ter)
}
}
}
})
}
答案 1 :(得分:1)
self.ret = []
self.ter = []
在开始附加新数据以清除阵列之前放置这些。