步骤1:我创建了tableview和custom 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数据源和委托:

 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

最佳答案

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

编辑1

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

关于ios - 对以下代码有一些想法? Tableview内的CollectionView返回相同的数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42344129/

10-16 00:39