Ios 表视图不显示单元格中的内容
我正在尝试使用alamofire和swifty json从堆栈交换api获取一些数据。我可以在日志中打印所需的数据,但当我运行应用程序时,模拟器只显示空单元格。我检查了标识符,并将原型单元格值设置为1`Ios 表视图不显示单元格中的内容,ios,uitableview,swift2,alamofire,swifty-json,Ios,Uitableview,Swift2,Alamofire,Swifty Json,我正在尝试使用alamofire和swifty json从堆栈交换api获取一些数据。我可以在日志中打印所需的数据,但当我运行应用程序时,模拟器只显示空单元格。我检查了标识符,并将原型单元格值设置为1` class MainTableViewController: UITableViewController,UISearchResultsUpdating { var searchKeyword: String = "questions" // empty array to
class MainTableViewController: UITableViewController,UISearchResultsUpdating {
var searchKeyword: String = "questions"
// empty array to store the search results
var searchResults: [SearchResult] = []
func alamofireFunction() {
Alamofire.request(.GET, "https://api.stackexchange.com/2.2/questions?=%20\(searchKeyword)%20viewpage=1&fromdate=1183075200&todate=2554416000&order=asc&sort=activity&tagged=ios&site=stackoverflow").responseJSON { (response) -> Void in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
for (var idx=0; idx<=json["items"].count; idx++) {
let result = SearchResult()
//print(json["items"][idx]["title"].stringValue)
result.name = json["items"][idx]["owner"]["display_name"].stringValue
result.question = json["items"][idx]["title"].stringValue
result.image = json["items"][idx]["owner"]["profile_image"].stringValue
self.searchResults.append(result)
}
}
case .Failure:
print("error")
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
alamofireFunction()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MainTableViewCell
cell.questionLabel.text = searchResults[indexPath.row].question
cell.nameLabel.text = searchResults[indexPath.row].name
return cell
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return searchResults.count
}`
class MainTableViewController:UITableViewController、UISearchResultsUpdate{
var searchKeyword:String=“疑问”
//用于存储搜索结果的空数组
var searchResults:[SearchResult]=[]
函数AlamofRefunction(){
请求(.GET,“https://api.stackexchange.com/2.2/questions?=%20\(searchKeyword)%20viewpage=1&fromdate=1183075200&todate=2554416000&order=asc&sort=activity&tagged=ios&site=stackoverflow”)。responseJSON{(response)->Void in
开关响应。结果{
成功案例:
如果let value=response.result.value{
让json=json(值)
对于(变量idx=0;idx UITableViewCell){
让cell=tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)作为!MainTableViewCell
cell.questionLabel.text=搜索结果[indexath.row]。问题
cell.namelab.text=searchResults[indexPath.row].name
返回单元
}
重写func numberOfSectionsInTableView(tableView:UITableView)->Int{
//#警告未完成执行,返回节数
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
//#警告未完成实现,返回行数
返回searchResults.count
}`
当数据源更改时,必须调用self.reloadData()。这将导致tableView调用数据源方法。必须调用self.reloadData()当您的数据源更改时。这将导致tableView调用数据源方法。这是因为您正在使用来自Url的请求,此过程在线程上执行,因此当您以这种方式获取数据时,必须调用tableView.reloadData()
要让数据源知道进程已经完成这是因为您使用的是来自Url的请求,此进程在线程上执行,因此当您以这种方式获取数据时,必须调用tableView.reloadData()
要让数据源知道流程已完成是否将委托和数据源设置为tableview?如果是,则在获取数据后重新加载tableview非常感谢,重新加载表格视图是否完成作业是否将委托和数据源设置为tableview?如果是,则在获取数据后重新加载tableview非常感谢,重新加载I不使用表视图完成了这项工作