如何获得一个Cell的编号?
How to have the number of a Cell?
我有一个 UICollectionView
和一些单元格。
我在这里做了一些测试:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
NSLog(@"nsindex path %@", indexPath);
cell.backgroundColor=[UIColor greenColor];
return cell;
}
这是一个单元格在日志中显示的示例:
nsindex path <NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}
如何从本例中的对象中提取值 1
(来自 path = 0 - 1
)?
您需要执行 indexPath.row
访问 table 单元格编号和 indexPath.item
访问集合单元格编号。
答案在 here:
中得到了最好的解释
indexPath.item(在 iOS 6.0 及更高版本中可用)
indexPath.row(在 iOS 2.0 及更高版本中可用)。
indexPath.item or indexPath.row use this for UICollectionView to get cell number
indexPath.row use this for UITableView to get cell number.
NSIndexPath 有两个属性:row
和 section
。 属性 row
表示当前section
中的row
个数。
我有一个 UICollectionView
和一些单元格。
我在这里做了一些测试:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
NSLog(@"nsindex path %@", indexPath);
cell.backgroundColor=[UIColor greenColor];
return cell;
}
这是一个单元格在日志中显示的示例:
nsindex path <NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}
如何从本例中的对象中提取值 1
(来自 path = 0 - 1
)?
您需要执行 indexPath.row
访问 table 单元格编号和 indexPath.item
访问集合单元格编号。
答案在 here:
中得到了最好的解释indexPath.item(在 iOS 6.0 及更高版本中可用) indexPath.row(在 iOS 2.0 及更高版本中可用)。
indexPath.item or indexPath.row use this for UICollectionView to get cell number
indexPath.row use this for UITableView to get cell number.
NSIndexPath 有两个属性:row
和 section
。 属性 row
表示当前section
中的row
个数。