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

时间:2016-09-22 02:45:04

标签: swift

您好我在制作UITableview时遇到了麻烦。我将cell作为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)
    }


}

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

以下是如何使用自定义创建的单元格。

注意:我没有使用StoryBoard,而且我已经在笔尖中创建了单元格。

CustoMCell类代码:

import UIKit

class ActivityTableViewCell: UITableViewCell {

// MARK: - Constants & variable
// your outlets if any

// MARK: - UITableViewCell methods

override func awakeFromNib() {
    super.awakeFromNib()

    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

// MARK: - helper methods

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> ActivityTableViewCell {
    let kActivityTableViewCellIdentifier = "kActivityTableViewCellIdentifier"
    tableView.registerNib(UINib(nibName: "ActivityTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kActivityTableViewCellIdentifier)

    let cell = tableView.dequeueReusableCellWithIdentifier(kActivityTableViewCellIdentifier, forIndexPath: indexPath) as! ActivityTableViewCell

    return cell
  }
}

这就是我在TableView中使用它的方式

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = ActivityTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)

// access your cell properties here
    return cell
}

答案 2 :(得分:0)

我通过像这样修改tablecellController解决了这个问题

     required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.alpha = 0

        UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseIn, animations: {
            self.alpha = 1
            }, completion: { finished in


        })  
 }