这意味着您很可能不会在 getPost() 方法中刷新 tableview。更新数据后,您需要致电self.postCollection.reloadData()。
你能发布 cellForRowAtIndexPath 的代码吗?您也可能没有正确考虑重复使用的单元格。
编辑:我相信我已经解决了
我看到了与您类似的行为,它与自动布局和大小类有关
对我来说,刷新控件显示在右侧,而不是居中:
模拟器:
bad-sim http://derrrick.com/stackoverflow/refreshcontrol/bad-sim.png
对你来说,它可能会在屏幕外显示。
注意在查看情节提要时这是如何显示的(“使用自动布局”和“使用大小类”都被选中)。
Xcode:
bad-xcode http://derrrick.com/stackoverflow/refreshcontrol/bad-xcode.png
一个解决方案是取消选中“使用自动布局”。然后你可能需要调整你的 tableview 的大小(我不得不删除它并重新添加它)。
那么它应该会正确显示。
模拟器:
good-sim http://derrrick.com/stackoverflow/refreshcontrol/good-sim.png
Xcode:
good-xcode http://derrrick.com/stackoverflow/refreshcontrol/good-xcode.png
最后,这是我的代码(虽然它缺少刷新方法:)
类 ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var postCollection: UITableView!
var myCollection = ["hi", "there", "bob"]
var refreshControl: UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
if let myTableView = self.postCollection {
self.postCollection!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.postCollection!.delegate = self
self.postCollection!.dataSource = self
self.refreshControl = UIRefreshControl(frame: CGRectMake(0, 0, 40, 40))
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
self.postCollection.insertSubview(self.refreshControl, atIndex: 0)
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = self.myCollection[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.myCollection.count
}
}