【问题标题】:UICollectionView cellForItemAtIndexPath indexPath row is nilUICollectionView cellForItemAtIndexPath indexPath 行为零
【发布时间】:2013-08-09 18:14:26
【问题描述】:

我第一次使用UICollectionView。我正在尝试获取每个单元格的“行”值 - 用作单元格文本字段的标识符/标签。当cellForItemAtIndexPath 被调用时,似乎[indexPath row]; 返回nil。当行为零时,如何获取单元格索引(或其他用作标识符的东西)?

注意 使用 iOS7,如果这有什么不同...

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Returns nil
    NSLog("Row: %d", indexPath.row);
}

有什么想法/提示吗?

【问题讨论】:

  • 它不返回 nil,它返回 0,对于第 0 行!
  • 同意,记录一个 double 永远不应该返回 nil。 0 != nil.and 如果确实,您的部分中只有一个项目,那么 Row 应该为索引 0 记录 0。如果您的部分中有 0 个项目,您将永远不会将此日志语句视为 cellForItemAtIndexPath: 永远不会调用。
  • 看来你是对的。日志显示 0 但po 出于某种原因显示为零。仍然有一些问题,所以我决定创建一个新问题,因为它并不完全相关。 stackoverflow.com/questions/18159462/…
  • @Anders po 代表“打印对象”。只需将p 用于基元即可。
  • @DavidRönnqvist 谢谢大卫,很高兴知道!

标签: ios cocoa-touch uicollectionview nsindexpath


【解决方案1】:

您不会在集合视图中使用行的概念。你需要从NSIndexPath使用这个方法:

+ (NSIndexPath *)indexPathForItem:(NSInteger)item inSection:(NSInteger)section;

注意NSIndexPathrow 属性是在UITableView.h 上声明的,而item 属性是在UICollectionView.h 上声明的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    相关资源
    最近更新 更多