我想使用 Parse 查询以向字符串添加字符串。然后我想将这些字符串放入 UITableView 的单元格中。但是,每次我运行应用程序时,似乎没有任何东西出现在我的桌子上。这是我的代码,如果有人可以帮助解释它可能没有出现的一些原因
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var friendsArray: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
var usrname = currentUser?.username
var query = PFQuery(className:"Relation")
query.whereKey("Sender", equalTo : usrname!)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
//println("Successfully retrieved \(objects) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
var friendName = object["Friend"] as! String
println(friendName)
self.friendsArray.append(friendName)
}
}
}
else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friendsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
cell.textLabel?.text = self.friendsArray[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
答案 0 :(得分:1)
您需要在[self.tableView reloadData];
来电的完成版块中致电findObjectsInBackground:
。
答案 1 :(得分:0)
当你致电findObjectsInBackgroundWithBlock
时,它正在按照其名称所说的去做,特别是它在后台运行。
同时,当它在后台运行时,你的表视图代码继续在前台运行,实际上是并行运行。
因此,您的viewDidLoad
函数将退出,接下来将调用numberOfRowsInSection
,但如果findObjectsInBackgroundWithBlock
尚未完成,那么friendsArray.count将为0。
答案 2 :(得分:0)
应用程序不知道何时会返回您的数据,因此您必须在成功接收数据后明确刷新 UITableView 。稍后使用[self.tableView reloadData]
重新加载 UITableView 。
UITableView 只会在加载 UIViewController 时加载一次。除非,您已经拥有 UIViewController 加载时的数据。否则,第一次行数将为0,并且不会调用委托方法cellForRowAtIndexPath
。
findObjectsInBackgroundWithBlock
是异步调用。它不像它的字面含义,它在后台运行。这意味着它不会阻止当前线程,因此用户仍然可以与应用程序进行交互。
答案 3 :(得分:0)
在if error == nil{...}
语句的末尾,加载您的表数据,如下所示:
if error == nil{
//All that other stuff...
tableView.reloadData()
}
另外,请确保您的tableView
已将delegate
和datasource
连接到您的视图控制器。这可能需要将其放在viewDidLoad
:
tableView.delegate = self
tableView.datasource = self
我还发现将tableView
声明为weak
这一点很奇怪,但这可能不相关。
尝试这些事情,如果您仍然遇到问题,请告诉我们。祝好运! :)
答案 4 :(得分:0)
试试这些事情。它会帮助你 获得元素数组后重新加载uitableView。 如果您使用故事板检查数据源插座 你没有使用storyboard或xib,然后你在编码中设置数据源
答案 5 :(得分:0)
嗨,你可以像这样使用
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
cell.textLabel?.text = app.str[indexPath.row]
print(" cell \(cell.textLabel?.text!)")
return cell
}
TableOutLet.reloadData()
答案 6 :(得分:0)
在 super.viewDidLoad()之后的 viewDidLoad()中的两行下面,将tableView与其委托和数据源相符合:
tableView.datasource = self tableView.delegate = self
然后按以下方式更新此代码:
如果让objects =对象为? [PFObject] { 对象中的对象{ var friendName = object [“Friend”] as!串 的println(FRIENDNAME) self.friendsArray.append(FRIENDNAME) } tableView.reloadData() }
之后在tableView委托方法中为numberOfRows()方法返回self.friendsArray.count,并将代码也放在cellForRow中。
答案 7 :(得分:0)
只需将行添加到数组中即可。
override func viewDidLoad() {
super.viewDidLoad()
var usrname = currentUser?.username
var query = PFQuery(className:"Relation")
query.whereKey("Sender", equalTo : usrname!)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
//println("Successfully retrieved \(objects) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
var friendName = object["Friend"] as! String
println(friendName)
self.friendsArray.append(friendName)
}
}
}
else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
或者您可以在最终阵列更新后重新加载表
答案 8 :(得分:-1)
正如另一个提示所提到的,你需要调用reloadData但你需要在主线程中调用它来尽快看到结果
dispatch_async(dispatch_get_main_queue()) {self.tableView.reloadData()}