无法获得第一个细胞的正确索引

时间:2017-11-08 11:42:00

标签: ios objective-c uicollectionview

尝试从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,但不能从第一个单元格中获取var oldPoly1Matrix; var canvas = new fabric.Canvas('c'); var srcOptions = { stroke: 'blue', fill: '', type: 'src' }; var destOptions = { stroke: 'red', fill: '', selectable: false, hasControls: false, hasBorders: false, type: 'dest' }; var poly1 = new fabric.Polygon([{ x: 60, y: 40 }, { x: 160, y: 40 }, { x: 160, y: 140 }, { x: 60, y: 140 }], srcOptions); var poly2 = new fabric.Polygon([{ x: 60, y: 300 }, { x: 160, y: 300 }, { x: 160, y: 400 }, { x: 60, y: 400 }], destOptions); canvas.add(poly1).add(poly2); oldPoly1Matrix = poly1.calcTransformMatrix(); var originalPoly2Matrix = poly2.calcTransformMatrix(); poly2.matrix = originalPoly2Matrix; function updatePoly2() { var newPoly1Matrix = poly1.calcTransformMatrix(); var oldPoly1MatrixInverted = fabric.util.invertTransform(oldPoly1Matrix); //newMatrix = oldMatrix * someMatrix //therefore,someMatrix = newMatrix / oldMatrix = newMatrix * inverse(oldMatrix); var diffMatrix = fabric.util.multiplyTransformMatrices(newPoly1Matrix, oldPoly1MatrixInverted); var oldPoly2Matrix = poly2.matrix; //Apply the same someMatrix to find out the new transform matrix for poly2. var newPoly2Matrix = fabric.util.multiplyTransformMatrices(oldPoly2Matrix, diffMatrix); var updatedPoints = poly2.get('points') .map(function(p) { return new fabric.Point(p.x - poly2.minX - poly2.width / 2, p.y - poly2.minY - poly2.height / 2); }) .map(function(p) { return fabric.util.transformPoint(p, newPoly2Matrix); }); oldPoly1Matrix = newPoly1Matrix; poly2.remove(); poly2 = new fabric.Polygon(updatedPoints, destOptions); poly2.matrix = newPoly2Matrix; canvas.add(poly2); }。点击第一个单元格时应用程序崩溃。

2 个答案:

答案 0 :(得分:1)

对于第一行,

indexPath.row为0,但0不是有效标记。 我认为你应该只使用非零值。

尝试即。

featureName.tag = indexPath.row + 1;

...

UILabel *featureName = [cell viewWithTag:indexPath.row+1];

答案 1 :(得分:0)

<强> 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 + 1;
    [cell addSubview:featureName];
didSelectItemAtIndexPath

中的

UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

UILabel *featureName = [cell viewWithTag:indexPath.row + 1 ];
featureName.textColor = [UIColor lightGrayColor];

希望这能解决您的问题:)