簡體   English   中英

從TableViewCell獲取TableView節標題,快速

[英]Getting the TableView Section Title from TableViewCell, swift

我有一個帶有兩種單元格的TableView ,它們都填充有CollectionView TableViewController我讓它們使用簡單的if語句顯示。

我的TableViewController

import UIKit
import RealmSwift
import Alamofire
import SwiftyJSON

let myGroupLive = DispatchGroup()
let myGroupCommunity = DispatchGroup() 

class HomeVTwoTableViewController: UITableViewController {

var headers = ["Live", "Channel1", "ChannelTwo", "Channel3", "Channel4", "Channel5", "Channel6"]

override func viewDidLoad() {
    super.viewDidLoad()
    DataController().fetchSomeDate(mode: "get")
    DataController().fetchSomeOtherData(mode: "get")
}
//MARK: Custom Tableview Headers
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return headers[section]
}

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){

    view.tintColor = UIColor.black
    let header = view as! UITableViewHeaderFooterView
    if section == 0 {
        header.textLabel?.textColor = UIColor.black
        view.tintColor = UIColor.white
    }
    else {
        view.tintColor = UIColor.groupTableViewBackground
    }
}

//MARK: DataSource Methods
override func numberOfSections(in tableView: UITableView) -> Int {
    return headers.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

//Choosing the responsible PrototypCell for the Sections
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellBig", for: indexPath) as! HomeVTwoTableViewCell

        return cell
    }
    else if indexPath.section == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellSmall", for: indexPath) as! HomeVTwoTableViewCellSmall

        return cell
    }

    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellSmall", for: indexPath) as! HomeVTwoTableViewCellSmall

        return cell
    }
}

//Set custom cell height, has to match the CollectionView height
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 0 {
        return 225.0
    }
    else {
        return 120.0
    }
  }
}

我的TableViewCellSmall

import UIKit
import RealmSwift

var communities: Results<Community>?

class HomeVTwoTableViewCellSmall: UITableViewCell{

@IBOutlet weak var collectionView: UICollectionView!
}

extension HomeVTwoTableViewCellSmall: UICollectionViewDataSource,UICollectionViewDelegate {

//MARK: Datasource Methods
func numberOfSections(in collectionView: UICollectionView) -> Int
{
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    return (communities?.count)!
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCellSmall", for: indexPath) as? HomeVTwoCollectionViewCellSmall else
    {
        fatalError("Cell has wrong type")
    }

//Here I want my Sorting Statement to make unique content per collection view
    //normal approach if no section is asked
    let url : String = (communities?[indexPath.row].pictureUri)!
    let name :String = (communities?[indexPath.row].communityName)!
    cell.titleLbl.text = name
    cell.imageView.downloadedFrom(link :"somelink")

    return cell
}


//MARK: Delegate Methods  
override func layoutSubviews() {
    myGroupCommunity.notify(queue: DispatchQueue.main, execute: {
        let realm = try! Realm()
        communities = realm.objects(Community.self)
        self.collectionView.dataSource = self
        self.collectionView.delegate   = self
    })

}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
do something
 }
}

現在的問題是,我希望“通道單元格”在CollectionView填充自定義的不同數據。 這意味着我需要某種密鑰才能在正確的單元格中獲取正確的數據。 我的方法是采用SectionHeader標題,但由於某些原因,我無法從TableViewCellSmall訪問它。 因此,我擁有所有單元格中的所有數據,並且沒有我的密鑰就無法對它們進行排序。

提前致謝。

據我了解,您需要用不同的內容填充每個單元格的collectionview,並為此需要標識單元格?

如果是這樣,我使用了下面對我有幫助的方法,您可以嘗試。

如果有疑問,請告訴我,以便我能提供幫助,希望我有所幫助:)

//TableViewCell Add

var didCollectionViewCellSelect: ((Int) -> Void)?

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

        // Configure the view for the selected state
    }


//TabelView Add
class myClass: UITableViewController
{
    var storedOffsets = [Int: CGFloat]()

    override func viewDidLoad()
    {
        super.viewDidLoad()

    }

  override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
  {
        guard let tableViewCell = cell as? myTableViewCell else { return }

        let secao = indexPath.section*1000 //Section
        let linha = indexPath.row //Row
        let posicao = secao+linha

        tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: posicao)
        tableViewCell.collectionViewOffset = storedOffsets[posicao] ?? 0
  }

  override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath)
  {
    guard let tableViewCell = cell as? myTableViewCell else { return }

    let secao = indexPath.section*1000 //Section
    let linha = indexPath.row //Row
    let posicao = secao+linha 

    storedOffsets[posicao] = tableViewCell.collectionViewOffset

  }

}       

//CollectionView        

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
  {
    let posicao = collectionView.tag
    let secao = Int(collectionView.tag/1000) //Section
    let linha = posicao-(secao*1000) //Row

    var qtd = 0

    if secao == 0 && arrStation.count > 0
    {
        qtd = arrStation.count
    }

    return qtd
  }

暫無
暫無

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

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