简体   繁体   English

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

[英]Display custom UITableViewCell from an array of cells

I have an UIViewController that creates a custom UITableViewCell and insert it into an array. 我有一个UIViewController ,它创建一个自定义UITableViewCell并将其插入到数组中。 Then I've a UITableViewController and I want to show cells from previous array. 然后,我有一个UITableViewController ,我想显示上一个数组中的单元格。 The problem is that in tableView(tableView:cellForRowAtIndexPath) I don't want to use dequeueReusableCellWithIdentifier because I just want to get the cells from the array and use them. 问题是在tableView(tableView:cellForRowAtIndexPath)我不想使用dequeueReusableCellWithIdentifier因为我只想从数组中获取单元格并使用它们。

Unfortunately this doesn't work. 不幸的是,这不起作用。 I read dequeueReusableCellWithIdentifier permits to save in memory only the necessary cells. 我阅读了dequeueReusableCellWithIdentifier允许仅将必要的单元格保存在内存中。

I'm thinking about a method to "push" my custom cells in that queue but I can't find anything. 我正在考虑一种将“自定义”单元格“推送”到该队列中的方法,但找不到任何东西。 So at the end I'm not able to show my custom cells. 因此,最后我无法显示我的自定义单元格。

Note: I can't obtain the cell using dequeueReusableCellWithIdentifier and than set each properties because one of them is a UIProgressView and it's updated within my custom cell class so I need to display that cell. 注意:我无法使用dequeueReusableCellWithIdentifier获得单元格,也无法设置每个属性,因为其中一个是UIProgressView并且在我的自定义单元格类中进行了更新,因此我需要显示该单元格。

This is the code within the UIViewController which create the custom cell and set it into the array: 这是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)

This is my custom 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
...
}

And finally this is my custom UITableView: 最后,这是我的自定义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]
}

}

Instead of adding UITableViewCells to your array you should probably add only the cell identifiers and then dequeue the cells in cellForRowAtIndexPath and do the set up there. 而不是将UITableViewCells添加到数组中,您可能应该仅添加单元格标识符,然后使cellForRowAtIndexPath的单元格出队并在那里进行设置。 If you can't retrieve the information to populate the cells in cellForRowAtIndexPath , then create a class or object to save the cell identifiers together with all the info you need to populate the cells (texts for labels, image file names for image views, etc). 如果您无法检索信息来填充cellForRowAtIndexPath的单元格,则创建一个类或对象来保存单元格标识符以及所有需要填充单元格的信息(标签文本,图像文件名,图像视图等)。 )。

Remember that table view cells are view objects and you should not include logic in view objects. 请记住,表视图单元格是视图对象,并且不应在视图对象中包含逻辑。 Put all the logic into a separate object and then just fill the cells as needed. 将所有逻辑放入单独的对象中,然后根据需要填充单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM