对下面的代码有一些想法? Tableview 内的CollectionView 返回相同的数据?

Posted

技术标签:

【中文标题】对下面的代码有一些想法? Tableview 内的CollectionView 返回相同的数据?【英文标题】:Give some idea about the below code? CollectionView inside the Tableview returns same data? 【发布时间】:2017-02-20 12:04:46 【问题描述】:

第一步:我创建了tableview和自定义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

带有集合视图扩展的自定义表格视图单元格:

      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 数据源和委托:

 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 节项目。其他项目不会显示。 即我得到的部分在collectionview数据源中有0。所以请有人帮我找出错误。 TableView 显示正确的部分,但在 collectionView 中返回第一部分和仅部分的值..

屏幕截图: Returns same data in CollectionView

【问题讨论】:

【参考方案1】:

cell?.lblProgress.text = dashSection[TableviewsindexPath.section].titles[CollectionviewsindexPath.row]应该是tableView的indexPath.section而不是collectionView的索引路径

编辑 1

【讨论】:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int print(section) return dashSection[section].titles.count 我不确定让我们测试一下cell?.lblProgress.text = dashSection[1].titles[indexPath.row] 用这段代码替换它并检查 兄弟你试过了吗? 是的,我已经这样做了,但是它崩溃了,因为没有索引 1 的值。numberOfItemsInSection 只返回 0。我该怎么办? 你能在 cellForItemAtIndex 路径中打印你的dashSection 吗?并将结果发送给我

以上是关于对下面的代码有一些想法? Tableview 内的CollectionView 返回相同的数据?的主要内容,如果未能解决你的问题,请参考以下文章

我对下面的代码有两个问题

从 API 填充 TableView 内的 CollectionView

TableView Height 无法在 IOS 中调整大小

UIScrollView 内的 UITableView 或使用 TableView Header

viewForHeaderInSection 在 xcode 8 中停止工作

Cell内的TableView动态高度问题