从单元格数组中显示自定义UITableViewCell

时间:2015-12-19 11:55:09

标签: ios swift uitableview

我有UIViewController创建自定义UITableViewCell并将其插入数组中。然后我有一个UITableViewController,我想显示前一个数组中的单元格。问题在于tableView(tableView:cellForRowAtIndexPath)我不想使用dequeueReusableCellWithIdentifier,因为我只想从数组中获取单元格并使用它们。

不幸的是,这不起作用。我读了dequeueReusableCellWithIdentifier许可证,只在内存中保存了必要的单元格。

我正在考虑一种在该队列中“推送”自定义单元格的方法,但我找不到任何东西。所以最后我无法显示自定义单元格。

注意:我无法使用dequeueReusableCellWithIdentifier获取单元格,而是设置每个属性,因为其中一个属性是UIProgressView并且它在我的自定义单元格类中更新,因此我需要显示该单元格。

这是UIViewController中创建自定义单元格并将其设置为数组的代码:

//here I create my custom cell (DownloadTVCell)
let cell = DownloadTVCell(style: UITableViewCellStyle.Default, reuseIdentifier: "DownloadRootTVC_IDcell")

cell.nomeFileInDownloadLabel.text = nomeCompletoMp3
//this is a method in DownloadTVCell that starts a download task
cell.createDownloadTask()

//here i got the navigationController in order to obtain the instance of 
//the UITableViewController. In this method when a user tap on the
//UITableViewController it's already instantiated
let nc = self.tabBarController!.viewControllers![1] as! UINavigationController
let drtvc = nc.topViewController as! DownloadRootTVC

//now drtvc is the instance of DOwnloadRootTVC which is my custom UITableViewController
//add the cell to the array
drtvc.listOfCellsDownlaod.append(cell)

这是我的自定义单元格:

class DownloadTVCell: MGSwipeTableCell, NSURLSessionDownloadDelegate {

//******************************************************************
//      IBOUtlet
//******************************************************************
@IBOutlet var nomeFileInDownloadLabel: UILabel!

@IBOutlet var quantitaScaricataLabel: UILabel!

@IBOutlet var progressView: UIProgressView!


private var downloadTask: NSURLSessionDownloadTask?

//******************************************************************
//      METODI
//******************************************************************


override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

    super.init(style: style, reuseIdentifier: reuseIdentifier)
    log("ACTION", text: "Metodo chiamato")
    build()
}

required init?(coder aDecoder: NSCoder) {

    super.init(coder: aDecoder)
    log("ACTION", text: "Metodo chiamato")

    build()
}



override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

private func build() {
    self.nomeFileInDownloadLabel = UILabel()
    self.quantitaScaricataLabel = UILabel()
    self.progressView = UIProgressView()
}

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

    // Configure the view for the selected state
}

//here there is some logic for download
...
}

最后这是我的自定义UITableView:

class DownloadRootTVC: UITableViewController {

//******************************************************************
//      PROPRIETA' GENERICHE
//******************************************************************
var listOfCellsDownlaod = [DownloadTVCell]()

//******************************************************************
//      METODI
//******************************************************************
override func viewDidLoad() {
    log(text: "self.listOfCellsDownlaod.count: \(self.listOfCellsDownlaod.count)")

    self.tableView.delegate = self
    self.tableView.dataSource = self

    self.refreshControl?.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadTableData:", name: "reload", object: nil)

    //self.tableView.registerClass(DownloadTVCell.self, forCellReuseIdentifier: "DownloadRootTVC_IDcell")

    super.viewDidLoad()
}

func refresh(sender:AnyObject) {
    self.tableView.reloadData()
    self.refreshControl?.endRefreshing()
}

func reloadTableData(notification: NSNotification) {
    tableView.reloadData()
}

override func viewDidAppear(animated: Bool) {
    self.tableView.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.listOfCellsDownlaod.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   //let cell = tableView.dequeueReusableCellWithIdentifier("DownloadRootTVC_IDcell", forIndexPath: indexPath) as! DownloadTVCell

    //I just want to return my custom cell
    return self.listOfCellsDownlaod[indexPath.row]
}

}

0 个答案:

没有答案