UICollectionView显示错误的单元格选定问题

时间:2015-02-16 09:32:14

标签: ios uicollectionview

我有一个CollectionView有很多单元格。一旦我选择其中一个显示相同的名称,但当我滚动时,我再次得到所有不同名称的单元格,但都是相同的。所以每个卷轴给我不同的名称,但都具有相同的名称。那么请问哪里可以成为我的问题?

我已经检查过这个UICollectionView showing wrong cells after scrolling - dequeue issue?,但它对我没有帮助。

BTW同样我有一个tableView并且它工作正常。

更新问题: 我如何在didSelectItemAtIndexPath UICollectionView NSIndexPath *path2 = [[_collection indexPathsForSelectedItems] lastObject]; ProductsCollectionViewCell *cell2 = (ProductsCollectionViewCell *) [_collection cellForItemAtIndexPath:path2]; 之外呼叫选定的电池标签?

解决方案:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

self.lblCollectionId = nil;
self.lblCollection = nil;

static NSString *identifier = @"cell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

_lblCollection = (UILabel *)[cell viewWithTag:102];
_lblCollection.text = [_arraySubCategory objectAtIndex:indexPath.row];

_lblCollectionId = (UILabel *)[cell.contentView viewWithTag:101];
_lblCollectionId.text = [_arrayID objectAtIndex:indexPath.row];

_imgCollection = (UIImageView *)[cell.contentView viewWithTag:100];

[_imgCollection sd_setImageWithURL:[NSURL URLWithString:[_arrayImage objectAtIndex:indexPath.row]]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *path = [_tbl indexPathForSelectedRow];
    ProductsTableViewCell *cell = (ProductsTableViewCell *) [_tbl cellForRowAtIndexPath:path];
    _selectedID = cell.lblID.text;

    _selectedIDCollection = _lblCollectionId.text;
    NSLong(@"%@ Selected", _selectedIDCollection);
}

这就是我如何做到的。

///////////////////////////////////////////////

static NSString *simpleTableIdentifier = @"proCell";
ProductsCollectionViewCell *proCell = [_collection dequeueReusableCellWithReuseIdentifier:simpleTableIdentifier forIndexPath:indexPath];

proCell.lblProduct.text = [_arraySubCategory objectAtIndex:indexPath.row];
proCell.lblID.text = [_arrayID objectAtIndex:indexPath.row];

为Ias更新:

{{1}}

1 个答案:

答案 0 :(得分:0)

使用标签是一个非常糟糕的主意,尤其是因为您在原型单元格上定义了标签,因此每个单元格都是相同的。 您必须创建一个自定义collectionViewCell类并将标签和imageView链接到IBOutlet,以便您可以正常访问它们:

CustomCollectionViewCell *cell = (CustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.label1.text = [_arraySubCategory objectAtIndex:indexPath.row];
cell.label2.text = [_arrayID objectAtIndex:indexPath.row];

检查this tutorial,它会逐步说明您应该如何做。