swift - UITableview 不显示我在 swift 中作为 nib 文件制作的单元格

标签 swift

您好,我在制作 UITableview 时遇到了麻烦。我将单元格制作为 nib 文件,并希望在 UITableView 中显示单元格。但是当我启动应用程序时,nib 文件不会显示在 UItableview 中。我不知道如何解决这个问题。来源如下

import UIKit

class DownLoadPlayViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet var tableview: UITableView!
    @IBOutlet var totalNumSizeInfo:UILabel!



    let cellID:String = "DownLoadPlayViewControllerCell"
    var downloadedContents: [Dictionary<String,String>] = []
    var showContents: Dictionary<String,String> = [:]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.registerClass(DownLoadPlayViewControllerCell.classForCoder(), forCellReuseIdentifier: "DownLoadPlayViewControllerCell")
//        self.tableview.registerClass(DownLoadPlayViewControllerCell.self, forCellReuseIdentifier: "DownLoadPlayViewControllerCell")

//        self.tableview.registerNib(UINib(nibName: "DownLoadPlayViewControllerCell", bundle: nil), forCellReuseIdentifier: "DownLoadPlayViewControllerCell")



        downloadedContents = getDownloadInformation()!;
        totalNumSizeInfo.text = "초기화 상태"

//        let contentInfo : Dictionary<String,String> = self.downloadedContents[0]
//        print(contentInfo["contentTitle"])
//        print(contentInfo["viewDate"])
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*Podlist에 있는 다운로드 파일정보를 가져온다. */
    func getDownloadInformation() -> [Dictionary<String,String>]?{
        var returnValue : [Dictionary<String,String>]? =
            NSUserDefaults.standardUserDefaults().objectForKey("downloadedContents") as? [Dictionary<String,String>]
        if((returnValue?.isEmpty) == true){
            returnValue = nil
        }
        return returnValue
    }

//    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
//        return 1
//    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return downloadedContents.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
//        let cell = tableView.dequeueReusableCellWithIdentifier("DownLoadPlayViewControllerCell", forIndexPath: indexPath) as! DownLoadPlayViewControllerCell
        let cell = tableView.dequeueReusableCellWithIdentifier("DownLoadPlayViewControllerCell") as! DownLoadPlayViewControllerCell
        let contentInfo : Dictionary<String,String> = self.downloadedContents[indexPath.row]
        cell.titleLabel?.text = contentInfo["contentTitle"]
        cell.dateLabel?.text  = contentInfo["viewDate"]

        return cell;
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        let contentInfo : Dictionary<String,String> = self.downloadedContents[indexPath.row]
        print(contentInfo["downLoadURL"])
    }

//MARK: ButtonClickArea

    @IBAction func closeButtonClick(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
    }


}

最佳答案

运行命令 downloadedContents = getDownloadInformation()! 后,您的 tableview 需要 reloadData。只需像这样运行 tableview.reloadData() 即可:

downloadedContents = getDownloadInformation()!;
tableview.reloadData() //---------> add new like here
totalNumSizeInfo.text = "초기화 상태"

关于swift - UITableview 不显示我在 swift 中作为 nib 文件制作的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39629445/

相关文章:

ios - iOS 框架的崩溃报告器

objective-c - 如何从 swift 访问枚举到 Objective C

ios - 包含值的 Firebase 结果数 - iOS Swift

ios - 如何在 UITableView 中重复使用单元格?

ios - 一页横向模式 swift

swift - DocumentId 包装器无法正常工作

ios - Swift 3 数组扩展错误

json - 如何使用 SwiftyJSON 从 Swift 中的 JSON 字符串中获取值?

ios - 显示具有 2 列大小相等的方形单元格的 UICollectionView

ios - UIDatePicker反射(reflect)选择的时间