gpt4 book ai didi

ios - 我如何从 UICollectionView 传递我的 id

转载 作者:行者123 更新时间:2023-11-29 11:55:52 26 4
gpt4 key购买 nike

当我单击 UICollectionView 中的按钮时,我想将我的食谱 ID 存储为 session ID。谁能给我解决方案。我将标签值设置为我的食谱 ID,但我不确定这是正确的方法。

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

[self.mySpinner stopAnimating];

static NSString *identifier = @"Cell";

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

UIButton *recipeid= (UIButton *)[cell.contentView viewWithTag:107];

[recipeid addTarget:self
action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
[recipeid setTag:(int)[[array objectAtIndex:indexPath.row] valueForKey:@"RecID"]];



}


-(IBAction)aMethod:(UIButton *)sender
{

[[NSUserDefaults standardUserDefaults] setObject:sender forKey:[NSNumber numberWithInt:(@"RecipeIdsession",sender.tag)]];

[[NSUserDefaults standardUserDefaults] synchronize];
[self performSegueWithIdentifier:@"recipedetail" sender:nil];
}

最佳答案

您可以像这样使用ButtonCGPoint 来获取IndexPath

-(void)aMethod:(UIButton *)sender{
CGPoint center= sender.center;
CGPoint point = [sender.superview convertPoint:center toView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
NSInteger recid = [[array objectAtIndex:indexPath.row] valueForKey:@"RecID"];
}

注意:cellForItemAtIndexPath 中删除 setTag 行,无需设置标签。

关于ios - 我如何从 UICollectionView 传递我的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39137481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com