2017-06-16 33 views
0

TableViewには2種類のセルがあり、どちらもCollectionViewで埋められています。 TableViewControllerでは、単純なifステートメントでそれらを表示させました。マイTableViewControllerTableViewCellからTableViewセクションタイトルを取得するswift

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 Titleを取ることですが、何らかの理由でTableViewCellSmallからアクセスできません。だから、私はすべてのセルのすべてのデータを持っていないし、私のキーなしでそれらを並べ替えることはできません。

ありがとうございました。

答えて

0

私はあなたが異なる内容の各セルのコレクションビューを記入する必要があることを理解しており、このためにセルを識別する必要がありますか?

もしそうなら、私は助けてくれた以下の方法を試してみることができます。

疑問に私はので、私は助けることができる知っているならば、私は私が助けた:)対応するための

//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 
    } 
+0

感謝を願っています。これは少し助けましたが、私の質問はひどく書かれたと思います。問題は、(識別子として)CollectionViewsのデータを埋め込むために、TableCellのセクションヘッダが必要なことです。私はグローバル変数でそれを解決しました。しかし、私はそれはかなり悪い練習だと思う。あなたのコードがこの問題を解決するかどうかはわかりません。 – Lukas

関連する問題