gpt4 book ai didi

ios - 如何获取indexpath值uiCollectionView iOS objective-c

转载 作者:行者123 更新时间:2023-11-29 00:18:49 25 4
gpt4 key购买 nike

您好,我想获取索引路径的值以从点击的单元格的数组中获取数据,我该怎么做?

下面是我的 Collection View 方法,self.iname 是包含名称的数组,cat 是类别 ID 我必须将所选图像类别 ID 发送到下一页我无法提取它

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

{


ColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];


if(self.iname!=0){

[cell setTag:indexPath.row]; // set tag to the indexPath.row so we can access it later

// add interactivity
UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];

[tap setNumberOfTapsRequired:1];

[cell addGestureRecognizer:tap];
// UITapGestureRecognizer *frameTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(frameTapGesture:)];
// [cell.dimg addGestureRecognizer:frameTapGesture];

NSString *fileName = [NSString stringWithFormat:@"%@",self.iname]; //objectAtIndex:indexPath];

NSLog(@"%@",fileName);

NSString *baseurl=[NSString stringWithFormat:@"http://test.kre8tives.com/barebon/upload/"];

NSDictionary *dict = self.iname[indexPath.row];

NSLog(@"%@", [self.iname objectAtIndex: indexPath.row]);

NSString *paths = [NSString stringWithFormat:@"%@%@", baseurl, dict];

NSLog(@"@@@@%@",paths);

[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:paths]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

NSLog(@"%@",response);


UIImage *imgage = [[UIImage alloc] initWithData:data];
UIImageView *dimg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
dimg.clipsToBounds = YES;
[cell.dimg setImage:imgage];


if(cell.dimg==!nil)
{
NSLog(@"Not nil");
}
else{
NSLog(@"nil");
}
if(cell.lbl==!nil)
{
NSLog(@"Not nil");
}
else{
NSLog(@"nil");
}

cell.dimg.image=imgage;
cell.lbl.text=[self.tempz objectAtIndex:indexPath.row];
NSLog(@"%@",[self.iname objectAtIndex:indexPath.row]);

}];
}
if(cell)
{
// self.catid=[self.categoryid objectAtIndex:indexPath.row];
//NSLog(@"%@",self.catid);
}
//if (cell.selected) {
// cell.backgroundColor = [UIColor blueColor]; // highlight selection
// }
// else
// {
// cell.backgroundColor = [UIColor redColor]; // Default color
// }
return cell;
}

这是我的点击手势方法

- (void)onButtonTapped:(UITapGestureRecognizer *)gestureRecognizer {
//UIImageView *myImage = (UIImageView *)gestureRecognizer.view;
// do stuff;
self.catid=self.categoryid;

NSLog(@"%@",self.catid);
CGPoint tapPoint = [gestureRecognizer locationInView:gestureRecognizer.view];
UIView *tappedView = [gestureRecognizer.view hitTest:tapPoint withEvent:nil];

if ([tappedView isKindOfClass:[UILabel class]]) {
NSLog(@"Found");
}
[self performSegueWithIdentifier:@"d2" sender:self];
NSLog(@"it works");
}

最佳答案

试试这个:

CGPoint position = [gestureRecognizer.view convertPoint:CGPointZero
toView:self.collectionView];
NSIndexPath *tappedIndexPath = [self.collectionView indexPathForItemAtPoint:position];

 NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint: tapPoint];

关于ios - 如何获取indexpath值uiCollectionView iOS objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44560773/

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