我为UITableView的单元格设置了值,但为什么它没有显示?

时间:2015-12-14 14:59:46

标签: ios swift uitableview

我在Main.storyboard中创建了一个UITableView和一个UITableVIewCell,并将它的dataSource和委托设置为ViewController。为什么UITableView在运行代码时没有显示文本。 另一个问题是在ViewLoad之前加载UITableView吗?如果不是为什么在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)
        }
    }


}

2 个答案:

答案 0 :(得分:1)

正如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,并确保所需方法正常运行。具体来说,numberOfRowsInSectionnumberOfSectionsInTableView都需要返回大于0的值。(在您发布的代码中,您缺少numberOfSectionsInTableView

答案 1 :(得分:1)

正如Lukas在评论中所说,你应该确保从你的cellForRowAtIndexPath方法中返回一个值,否则它将拒绝构建。如果你已经完成了这项工作并且仍然没有看到任何单元格,那可能是因为numberOfRowsInSectionnumberOfSectionsInTableView返回0,所以你应该确保它们返回正整数。