gpt4 book ai didi

ios - UITableView 不显示单元格(使用 swift )

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:04 24 4
gpt4 key购买 nike

我正在使用 xcode v6.4。我想将我从 AWS DynamoDB 获得的所有用户名显示到 TableView 中。我试过下面的代码

import UIKit
class messagesTableViewController: UITableViewController {

var myData: Array<AnyObject> = []
override func viewDidLoad() {
super.viewDidLoad()


let exp = AWSDynamoDBScanExpression()

// exp.valueForKey("name")
// exp.scanFilter = [ "date" : cond ]

self.scan(exp).continueWithSuccessBlock({ (task: AWSTask!) -> AWSTask! in
NSLog("Scan multiple values - success")
let results = task.result as! AWSDynamoDBPaginatedOutput
for r in results.items {
let myItem: Item = r as! Item

println(myItem.name)
self.myData.append(myItem.name)

println("\(self.myData)")

}
return nil
})
}

func scan(expression : AWSDynamoDBScanExpression) -> AWSTask! {

let mapper = AWSDynamoDBObjectMapper.defaultDynamoDBObjectMapper()
return mapper.scan(Item.self, expression: expression)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
println("\(myData.count)")
return myData.count

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellID: String = "cell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! inboxTableViewCell
dispatch_async(dispatch_get_main_queue(), {


cell.nameLabel.text = self.myData[indexPath.row] as? String

})



return cell

}

这里,打印命令 'println("(self.myData)")' 打印了正确的数据,但相同的数据没有显示在 TableView 中。

此外,当我将静态值添加到数组时,相同的代码可以正常工作并在表格 View 中显示单元格。例如,下面是我的代码:

import UIKit

class messagesTableViewController: UITableViewController {


var myDummy: Array<AnyObject> = []

override func viewDidLoad() {
super.viewDidLoad()

var i = 1
myDummy = ["Amit", "Kushal", "Saurabh", "Harsh", "Swar"]

}



override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return myDummy.count

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellID: String = "cell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! inboxTableViewCell
dispatch_async(dispatch_get_main_queue(), {


cell.nameLabel.text = self.myDummy[indexPath.row] as? String

})



return cell

}

如果有人能指导我解决这个问题,那将是一个很大的帮助。

最佳答案

我终于明白了。这是更新后的代码。

import UIKit

class messagesTableViewController: UITableViewController {

var myData: Array<AnyObject> = []


override func viewDidLoad() {
super.viewDidLoad()

var i = 1

let exp = AWSDynamoDBScanExpression()

// exp.valueForKey("name")
// exp.scanFilter = [ "date" : cond ]

self.scan(exp).continueWithSuccessBlock({ (task: AWSTask!) -> AWSTask! in
NSLog("Scan multiple values - success")
let results = task.result as! AWSDynamoDBPaginatedOutput
for r in results.items {
let myItem: Item = r as! Item

println(myItem.name)
self.myData.append(myItem.name)

println("\(self.myData)")

}

// This is the change in code. It needs to be added because the AWS scan gathers the data in a background queue and we need to get back to the main queue after the background task is completed and then reload the table view.
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
});
return nil
})

}

func scan(expression : AWSDynamoDBScanExpression) -> AWSTask! {

let mapper = AWSDynamoDBObjectMapper.defaultDynamoDBObjectMapper()
return mapper.scan(Item.self, expression: expression)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
println("\(myData.count)")
return myData.count

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellID: String = "cell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! inboxTableViewCell
dispatch_async(dispatch_get_main_queue(), {


cell.nameLabel.text = self.myData[indexPath.row] as? String

})



return cell

}



}

问题中的代码没有运行,因为AWS扫描在后台队列中收集数据,我们需要在后台任务完成后返回主队列,然后重新加载 TableView 。

关于ios - UITableView 不显示单元格(使用 swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31212277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com