Ios 给出一些关于下面代码的想法?Tableview中的CollectionView是否返回相同的数据?
步骤1:我创建了tableview和自定义tableview单元格,下面是代码** 结构类别:Ios 给出一些关于下面代码的想法?Tableview中的CollectionView是否返回相同的数据?,ios,iphone,swift,swift3,Ios,Iphone,Swift,Swift3,步骤1:我创建了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
struct DashSection {
var heading : String
var titles : [String]
var images : [String]
init(title : String, subtitle : [String], icons : [String]) {
heading = title
titles = subtitle
images = icons
}
}
}
具有集合视图扩展名的自定义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节项。其他项目将不显示。
i、 我得到的是collectionview数据源中的节有0。所以请有人帮我找出错误。
TableView显示了正确的部分,但在collectionView中返回第一部分以及仅部分的值
屏幕截图:
cell?.lblProgress.text=dashSection[TableviewsindexPath.section]。标题[CollectionviewsindexPath.row]
应为tableView的indexPath.section,而不是collectionView的索引路径
编辑1
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{print(section)return dashSection[section].titles.count}我不确定是否让我们测试它
cell?.lblProgress.text=dashSection[1]。titles[indexath.row]
用此代码替换它,并检查是否尝试过?是,我已经这样做了,但是它崩溃了,因为没有索引1的值。numberOfItemsInSection仅返回0。我能做什么?您能在cellForItemAtIndex路径中打印您的dashSection
?把结果寄给我
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
}