2017-02-20 17 views
0

Шаг1: Я создал TableView и пользовательские TableView ячейка здесь код **Опишите, пожалуйста, код ниже. CollectionView внутри Tableview возвращает те же данные?

Структура класса:.

struct DashSection { 
var heading : String 
var titles : [String] 
var images : [String] 

init(title : String, subtitle : [String], icons : [String]) { 
    heading = title 
    titles = subtitle 
    images = icons 
} 

}

class DashSectionData 
{ 
func getSectionData() -> [DashSection] { 
    var arrDash = [DashSection]() 
    let firstDic = DashSection(title: "MY ACCOUNT", subtitle: ["Dashboard","Incidents","CAPA","Audits","Reports","Health Register"], icons: ["Heart","DownArrow","DownArrow","DownArrow","DownArrow","DownArrow"]) 
     let secondDic = DashSection(title: "COMPLIANCE", subtitle: ["Videos","Documents","Training","Safety IQ"], icons: ["DownArrow","DownArrow","DownArrow","DownArrow","DownArrow","DownArrow"]) 
     let thirdDic = DashSection(title: "RESOURCES", subtitle: ["Videos","Documents","Training","Safety IQ"], icons: ["DownArrow","DownArrow","DownArrow","DownArrow"]) 
    let fourthDic = DashSection(title: "SETTINGS", subtitle: ["Sites","Users","Forms"], icons: ["DownArrow","DownArrow","DownArrow"]) 
    arrDash.append(firstDic) 
    arrDash.append(secondDic) 
    arrDash.append(thirdDic) 
    arrDash.append(fourthDic) 
    return arrDash 
} 

}

Выборочная TableView ячейка с расширением коллекции:

 var dashSection : [DashSection] = DashSectionData().getSectionData() 

    class IncidentTableViewCell: UITableViewCell { 

@IBOutlet var collectionView: UICollectionView! 
@IBOutlet var viewFull: UIView! 
override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
    self.layer.shadowColor = UIColor.lightGray.cgColor 
    self.layer.shadowOffset = CGSize(width: 0, height: 2.0) 
    self.layer.shadowRadius = 2.0 
    self.layer.shadowOpacity = 1.0 
    self.layer.masksToBounds = false 
    self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.contentView.layer.cornerRadius).cgPath 
    collectionView.dataSource = self 
    collectionView.delegate = self 
    // NotificationCenter.default.addObserver(self, selector: #selector(self.reloadCollectionView), name: .notificationReloadCollection, object: nil) 
} 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
    // Configure the view for the selected state 
} 
} 

extension IncidentTableViewCell : UICollectionViewDataSource 
{ 
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    print(section) 
    return dashSection[section].titles.count 
} 
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? IncidentProgressCollectionViewCell 
    cell?.lblProgress.text = dashSection[indexPath.section].titles[indexPath.row] 
    cell?.progressView.value = 0.0 
    _ = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false, block: { (Timer) in 
     UIView.animate(withDuration: 6.0) { 
      cell?.progressView.value = CGFloat(indexPath.row * 10) 
     } 
    }) 
    // callAnimation(cell: cell!, index: indexPath.row, progress: (cell?.progressView)!) 
    return cell! 
} 

func callAnimation(cell: IncidentProgressCollectionViewCell,index:Int,progress: MBCircularProgressBarView) 
{ 
    UIView.animate(withDuration: 1.0) { 

     cell.progressView.value = CGFloat(index * 10) 


    } 
} 

func reloadCollectionView() 
{ 
    if(cellIsSelected.isFirstTime == true) 
    { 
     self.collectionView.reloadData() 
    } 
} 

}

Tableview DataSource и делегат:

func numberOfSections(in tableView: UITableView) -> Int 
{ 
    return dashSection!.count 
    } 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return dashSection?[section].heading 
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? IncidentTableViewCell 
    let path = UIBezierPath(roundedRect:(cell?.bounds)!, 
          byRoundingCorners:[.bottomRight, .bottomLeft], 
          cornerRadii: CGSize(width: 10, height: 10)) 
    let maskLayer = CAShapeLayer() 
    maskLayer.path = path.cgPath 
    cell?.layer.mask = maskLayer 
    return cell! 
} 

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let headerView = UIView() 
    let label : UILabel? 
    if(section == 0) 
    { 
     headerView.frame = CGRect(x: 0, y: 0, width: self.tblView.frame.size.width, height: 60) 
     label = UILabel(frame: CGRect(x: 17, y: headerView.center.y - 12.5, width: view.frame.size.width, height: 25)) 
    } 
    else 
    { 
     headerView.frame = CGRect(x: 0, y: 0, width: self.tblView.frame.size.width, height: 40) 
     label = UILabel(frame: CGRect(x: 17, y: headerView.center.y - 12.5, width: view.frame.size.width, height: 25)) 
    } 
    headerView.backgroundColor = UIColor(red: 250/255, green: 250/255, blue: 250/255, alpha: 1.0) 

    let path = UIBezierPath(roundedRect:headerView.bounds, 
          byRoundingCorners:[.topRight, .topLeft], 
          cornerRadii: CGSize(width: 10, height: 10)) 
    let maskLayer = CAShapeLayer() 
    maskLayer.path = path.cgPath 
    headerView.layer.mask = maskLayer 
    // 
    // 

    // let label = UILabel(frame: CGRect(x: 17, y: 7, width: view.frame.size.width, height: 25)) 
    label?.text = dashSection?[section].heading 
    label?.textColor = UIColor.orange 
    headerView.addSubview(label!) 
    return headerView 
} 

Пожалуйста, обратитесь к ниже прилагается снимок экрана. На этом экране вы получите раздел 0 и верните только раздел 0-й позиции. Другие предметы не отображаются. i.e Я получаю раздел имеет 0 в источнике данных коллекции. Поэтому, пожалуйста, кто-нибудь поможет мне определить ошибку. TableView показывает правильные секции, но в CollectionView возвращает первую секцию и значение для только секций ..

выстрел экрана: Returns same data in CollectionView

ответ

0

cell?.lblProgress.text = dashSection[TableviewsindexPath.section].titles[CollectionviewsindexPath.row] должен быть indexPath.section из Tableview не индекс пути CollectionView

Edit 1

enter image description here

+0

функ CollectionView (_ CollectionView: UICollection View, numberOfItemsInSection: Int) -> Int { print (раздел) return dashSection [section] .titles.count } – Sureshkumar

+0

В этом вышеприведенном коде всегда возвращается 0. – Sureshkumar

+0

Я не уверен. Давайте проверим его 'cell ?. lblProgress.text = dashSection [1] .titles [indexPath.row] 'замените его на этот код и проверьте – Koushik