簡體   English   中英

顯示單元格數組中的自定義UITableViewCell

[英]Display custom UITableViewCell from an array of cells

我有一個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]
}

}

而不是將UITableViewCells添加到數組中,您可能應該僅添加單元格標識符,然后使cellForRowAtIndexPath的單元格出隊並在那里進行設置。 如果您無法檢索信息來填充cellForRowAtIndexPath的單元格,則創建一個類或對象來保存單元格標識符以及所有需要填充單元格的信息(標簽文本,圖像文件名,圖像視圖等)。 )。

請記住,表視圖單元格是視圖對象,並且不應在視圖對象中包含邏輯。 將所有邏輯放入單獨的對象中,然后根據需要填充單元格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM