gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:39:54 25 4
gpt4 key购买 nike

第1步:我创建了表格 View 和自定义表格 View 单元格,这里是代码。**

结构类:

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
}

}

带有 Collection View 扩展的自定义表格 View 单元:

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

 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

enter image description here

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com