【发布时间】:2015-09-28 06:53:39
【问题描述】:
所以我有这个问题,我必须在两个集合视图和一个表视图上加载一个实体。问题是,主要我只需要在 tableview 所在的 VC 上加载一个实体。该数据实体具有以下参数,即:
- 发动机类型(柴油或汽油)
- 汽车颜色
- 最大速度
- 生产年份
现在,重点是,根据汽车引擎的类型为 tableview 上的单元格着色。因此,如果数据实体“汽车”的布尔值为“isEngineDiesel”=true,则单元格将为橙色,如果为 false,则为浅蓝色。这工作得很好,一个简单的 if 语句,用于加载此类单元格的表视图委托方法。但是,现在我必须实现另一个具有两个集合视图的 VC,其中第一个仅加载柴油引擎汽车实体,另一个是汽油类型。 所以我想这个问题在这里已经很清楚了。我怎样才能做到这一点?因为经过无数小时的试验,我唯一的想法是两个制作两个实体,一个是 DieselCar,另一个是 PetrolCar。但这意味着要更改完整的数据结构,而且实际上我需要两个而不是一个表视图,这似乎不是一个好主意,因为它会“溢出”所有数据。
那么...有什么想法吗?
编辑:
到目前为止,我只设法获得了单元格标题,但单元格数量的返回值仍然是如何解决的一个谜。 集合视图委托的代码:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell
if let carData = fetchedResultsController?.objectAtIndexPath(indexPath) as? Car {
cell.layer.cornerRadius = 20
cell.layer.masksToBounds = true
if collectionView == collectionViewDiesel {
if carData.isEngineDiesel == true {
cell.backgroundColor = UIColor(netHex: 0x8DF060)
// Display the cell name
cell.cellTitle.text = carData.cellTitle
}
}else if collectionView == collectionViewPetrol {
if carData.isCarDiesel == false {
cell.backgroundColor = UIColor(netHex: 0xEB9B2D)
// Display the cell name
cell.cellTitle.text = carData.cellTitle
}
}
}
return cell
}
这是我需要回答的方法:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
【问题讨论】:
标签: ios swift uitableview core-data uicollectionview