我已经在UITableView和UITableVIewCell中创建了Main.storyboard,并设置了它的dataSource,并且委托给ViewController .Why UITableView在运行代码时没有显示文本。另一个问题是,UITableView在ViewLoad之前加载吗?如果不是,为什么在func didRecieveResults()中,tableData数组可以实现数据,但是在func tableView()中是0
整个代码如下
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,HttpProtocol {
@IBOutlet weak var tv: UITableView!
@IBOutlet weak var iv: UIImageView!
@IBOutlet weak var playTime: UILabel!
@IBOutlet weak var progressView: UIProgressView!
var eHttp:HttpController = HttpController()
var tableData:NSArray = NSArray()
var channelData:NSArray = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
eHttp.delegate = self
eHttp.onSearch("http://www.douban.com/j/app/radio/channels")
eHttp.onSearch("http://douban.fm/j/mine/playlist?channel=0")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
println("tableData.count:\(channelData)")
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell!{
let cell = UITableViewCell(style:UITableViewCellStyle.Subtitle,reuseIdentifier:"douban")
let rowData:NSDictionary = self.tableData[indexPath.row] as! NSDictionary
cell.textLabel!.text = "hehehehe"//rowData["title"] as! String
cell.detailTextLabel!.text = "adasdasda"//rowData["artist"] as! String
return cell
}
func didRecieveResults(results:NSDictionary){
if (results["song"] != nil){
self.tableData = results["song"] as! NSArray
println(tableData)
}else if (results["channels"] != nil){
self.channelData = results["channels"] as! NSArray
// println(channelData)
}
}
}发布于 2015-12-14 15:07:48
正如Lukas所指出的,您需要在方法的末尾返回UITableViewCell。
实际上,您发布的内容甚至不应该编译,所以我想知道您是否错误地发布了示例代码。
首先要尝试并实际返回单元格,将代码更新为:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell!
{
let cell = UITableViewCell(style:UITableViewCellStyle.Subtitle,reuseIdentifier:"douban")
let rowData:NSDictionary = self.tableData[indexPath.row] as! NSDictionary
cell.textLabel!.text = "hehehehe"//rowData["title"] as! String
cell.detailTextLabel!.text = "adasdasda"//rowData["artist"] as! String
// YOU ARE MISSING THIS LINE
return cell
}还请确保正确设置了UITableViewDatasource,并确保所需的方法正在运行。具体来说,numberOfRowsInSection和numberOfSectionsInTableView都需要返回大于0的值。(在您发布的代码中,您缺少了numberOfSectionsInTableView)
发布于 2015-12-14 15:08:25
正如Lukas在评论中所说的,您应该确保从您的cellForRowAtIndexPath方法返回一个值,否则它将拒绝构建。如果您已经这样做了,但仍然没有看到任何单元格,这可能是因为numberOfRowsInSection或numberOfSectionsInTableView返回0,所以您应该确保它们返回一个正整数。
https://stackoverflow.com/questions/34270138
复制相似问题