首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我已经为UITableView的单元格设置了值,但是为什么它没有显示?

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

Stack Overflow用户
提问于 2015-12-14 14:59:46
回答 2查看 680关注 0票数 3

我已经在UITableView和UITableVIewCell中创建了Main.storyboard,并设置了它的dataSource,并且委托给ViewController .Why UITableView在运行代码时没有显示文本。另一个问题是,UITableView在ViewLoad之前加载吗?如果不是,为什么在func didRecieveResults()中,tableData数组可以实现数据,但是在func tableView()中是0

整个代码如下

代码语言:javascript
复制
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)
        }
    }


}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-14 15:07:48

正如Lukas所指出的,您需要在方法的末尾返回UITableViewCell

实际上,您发布的内容甚至不应该编译,所以我想知道您是否错误地发布了示例代码。

首先要尝试并实际返回单元格,将代码更新为:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-12-14 15:08:25

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34270138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档