ios - UICollectionView 帮助

标签 ios objective-c uicollectionview

我正在创建一个应用程序来为哈雷戴维森爱好者制定维护计划。 目前计划目标是iPad系列。

我希望显示一个网格,显示每次燃料购买的详细信息。下图有望显示我正在努力实现的目标。

enter image description here

为此,我正在使用 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;
}

enter image description here

我希望每条信息都有一个单元格,但不确定如何使用 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/

相关文章:

ios - 如何在 iOS 6 中使用具有多个 subview 的现有自定义 View 自动布局

ios - UILabel 在 iOS7 中有一个奇怪的灰色顶线/边框,我该如何删除它?

ios - 为什么当我将滚动方向从垂直更改为水平时, Collection View 会崩溃?

ios - 具有方向的单元格位置

ios - View 更新后保持对 View /数据模型的引用

ios - 我们可以在 ios 的单个应用程序中有两个不同的订阅吗

iOS8/模拟器 - SFHFKeychainUtils 错误,SFHFKeychainUtilsErrorDomain 错误 -34018

ios - 将流字节转换为字符串

ios - 单元格或标题 View 高度的 UITableViewAutomaticDimension 导致 UITableView insertRowsAtIndexPaths() 崩溃

iphone - 添加更多单元格 UICollectionView