【问题标题】:I have set value for cell of UITableView but why it didn't display?我已经为 UITableView 的单元格设置了值,但为什么它没有显示?
【发布时间】: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


【解决方案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

【讨论】:

    【解决方案2】:

    正如 Lukas 在评论中所说,您应该确保从您的 cellForRowAtIndexPath 方法返回一个值,否则它将拒绝构建。如果您已经这样做了,但仍然看不到任何单元格,这可能是因为 numberOfRowsInSectionnumberOfSectionsInTableView 返回 0,因此您应该确保它们返回一个正整数。

    【讨论】:

    • 对不起,我忘了复制返回单元格,不是这个原因
    • 我已经提出了一个编辑请求来说明这一点。你能确认你肯定会为我提到的两种方法返回一个正整数吗?
    • 谢谢,你是对的,numberOfRowsInSection 返回 0
    猜你喜欢
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 2019-09-13
    • 2011-05-15
    • 2022-11-20
    相关资源
    最近更新 更多