iOS-如何指定每个UICollectionViewCell的索引路径?

iOS-如何指定每个UICollectionViewCell的索引路径?,ios,objective-c,uicollectionview,uicollectionviewcell,nsindexpath,Ios,Objective C,Uicollectionview,Uicollectionviewcell,Nsindexpath,我正在尝试使用CollectionView作为导航按钮的良好布局。我有一个很大的If…else语句,但我得到了一个错误: 在类型为“UICollectionViewCell*”的对象上未找到语义问题属性“indexPath” 下面是给出错误的代码。必须有更好的方法来获取单元格的索引路径 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPa

我正在尝试使用CollectionView作为导航按钮的良好布局。我有一个很大的If…else语句,但我得到了一个错误:

在类型为“UICollectionViewCell*”的对象上未找到语义问题属性“indexPath”

下面是给出错误的代码。必须有更好的方法来获取单元格的索引路径

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

if (_navCell.indexPath = @"0 - 0")
{
    [self performSegueWithIdentifier:@"toPersonnel" sender:self];
}
else if (_navCell.indexPath = @"0 - 1")
{
    [self performSegueWithIdentifier:@"toEquipment" sender:self];
}
else if (_navCell.indexPath = @"0 - 2")
{
    [self performSegueWithIdentifier:@"toTasks" sender:self];
}
else if (_navCell.indexPath = @"0 - 3")
{
    [self performSegueWithIdentifier:@"toTriage" sender:self];
}
else if (_navCell.indexPath = @"0 - 4")
{
    [self performSegueWithIdentifier:@"toLogs" sender:self];
}
else if (_navCell.indexPath = @"0 - 5")
{
    [self performSegueWithIdentifier:@"toMapping" sender:self];
}
else if (_navCell.indexPath = @"0 - 6")
{
    [self performSegueWithIdentifier:@"toHeadsUp" sender:self];
}
else if (_navCell.indexPath = @"0 - 7")
{
    [self performSegueWithIdentifier:@"toMessenger" sender:self];
}
}
“我的收藏”视图的其他代码:

@interface mainViewController ()
  @property (strong, nonatomic) IBOutlet UICollectionViewCell *navCell;
@end
viewdidload:

- (void)viewDidLoad
{
[super viewDidLoad];
navItems = [[NSMutableArray alloc]init];
[navItems addObject:@"personnel"];
[navItems addObject:@"equipment"];
[navItems addObject:@"tasks"];
[navItems addObject:@"triage"];
[navItems addObject:@"logs"];
[navItems addObject:@"mapping"];
[navItems addObject:@"headsup"];
[navItems addObject:@"messenger"];
}
集合视图方法

-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}

-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [navItems count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIImageView *image = (UIImageView *)[cell viewWithTag:100];
image.image = [UIImage imageNamed:[navItems objectAtIndex:indexPath.row]];

return cell;
}
有人能告诉我如何找到不同单元格的索引,这样我就可以把它们连接到我的序列上吗


谢谢

您的
didSelectRow…
方法有很多问题

  • 该方法有一个
    indexPath
    参数
  • 您用于比较索引路径的检查完全错误
  • 使用数组
  • 试试这个:

    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
        NSString *segue = segues[indexPath.row];
        [self performSegueWithIdentifier:segue sender:self];
    }
    
    segues
    添加一个实例变量作为
    NSArray*segues

    然后在
    viewDidLoad
    中,您可以对其进行初始化:

    segues = @[ @"toPersonnel", @"toEquipment", @"toTasks" /* and the rest */ ];
    

    您是否注意到
    indepath
    是该方法的一个参数?为什么在
    if
    中只有一个“=”?如果您只有一个部分,请改为执行If
    If([indexPath row]==x)
    ,其中
    x
    是一个
    int
    。我知道我的方法是错误的。请不要否决投票。如果没有错的话,我不需要在这里问正确的方法。是的,这个方法非常有效。非常感谢。我只是误解了类似问题的答案。