尝试从didSelectItemAtIndexPath中的uilabel
访问cellForItemAtIndexPath
,但无法获取第一个单元格。这是我的代码
在cellForItemAtIndexPath
中
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UILabel *featureName = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, cell.frame.size.width-10, cell.frame.size.height-10)];
featureName.tag = indexPath.row;
[cell addSubview:featureName];
在
didSelectItemAtIndexPath
中UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
UILabel *featureName = [cell viewWithTag:indexPath.row];
featureName.textColor = [UIColor lightGrayColor];
可以从所有单元格获得
label.tag
,但不能从第一个单元格获得。点击第一个单元格时,应用程序崩溃。 最佳答案
第一行的indexPath.row
为0,但0不是有效标记。
我认为您应该只使用非零值。
尝试即
featureName.tag = indexPath.row + 1;
...
UILabel *featureName = [cell viewWithTag:indexPath.row+1];