【发布时间】:2015-12-14 14:59:46
【问题描述】:
我在 Main.storyboard 中创建了一个 UITableView 和一个 UITableVIewCell 并将其设置为 dataSource 并委托给 ViewController 。为什么当我运行 代码 时 UITableView 没有显示文本。 另一个问题是 UITableView 在 ViewLoad 之前加载吗?如果不是为什么在 func didRecieveResults() 中 tableData 的数组可以实现数据但在 func tableView() 中它是 nil
全部代码如下
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)
}
}
}
【问题讨论】:
-
你少了一行。返回单元格
-
如果以上评论对您没有帮助,请添加整个课程代码
-
两个答案都是正确的,我只想补充一点,您还应该提供 UITablveViewDataSource 连接
标签: ios swift uitableview