我正在创建一个应用程序来为哈雷戴维森爱好者制定维护计划。 目前计划目标是iPad系列。
我希望显示一个网格,显示每次燃料购买的详细信息。下图有望显示我正在努力实现的目标。
为此,我正在使用 UICollectionView,因为我认为没有其他 View 可以满足我的需求。 下面的代码说明了我是如何做到这一点的——但我认为这是一种相当笨拙的方法:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FuelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor lightTextColor];
cell.cellLabel.text = [NSString stringWithFormat:@" %@ %@ %@ %@",self.fuelDetailsForSelectedBike[indexPath.row][0],self.fuelDetailsForSelectedBike[indexPath.row][1],self.fuelDetailsForSelectedBike[indexPath.row][2],self.fuelDetailsForSelectedBike[indexPath.row][3]];
return cell;
}
我希望每条信息都有一个单元格,但不确定如何使用 UICollectionView 实现这一点。
最佳答案
您已经创建了一个自定义单元格 FuelCollectionViewCell
,这是一个很好的起点。
您的 Storyboard 中可能有一个带有子类 FuelCollectionViewCell
的原型(prototype)单元,对吗?
在这个单元格中,添加 4 个 UILabel 作为您的设计排成一行,并使用简单的拖动+alt 将它们“链接”到您的子类 .h 文件(就像您对 cellLabel
所做的那样) .
并更新您的 cellForItemAtIndexPath
方法,例如:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FuelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor lightTextColor];
cell.dateLabel.text = self.fuelDetailsForSelectedBike[indexPath.row][0];
cell.litresLabel.text = self.fuelDetailsForSelectedBike[indexPath.row][1];
cell.distanceLabel.text = self.fuelDetailsForSelectedBike[indexPath.row][2];
cell.costLabel.text = self.fuelDetailsForSelectedBike[indexPath.row][3];
return cell;
}
关于ios - UICollectionView 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38263503/