【问题标题】:Sending a tag value to NSMutableArray objectAtIndex returns null向 NSMutableArray objectAtIndex 发送标签值返回 null
【发布时间】:2016-01-18 11:22:21
【问题描述】:

我有一个产品详细信息列表作为 NSMutableArray 中的对象和产品图像列表作为图库,我需要在单击缩略图时加载完整图像。为此,我设置标签并在点击特定图像时获得准确的标签值。问题是我无法获取数组对象。我使用手动数字进行测试,数组完美地返回对象。我在这里做错了什么?

标签设置代码:

for(int index=0; index < [self.sliderImages count]; index++) 
{ 
   UIImageView *imageView = [[UIImageView alloc]init]; 
   imageView.frame = CGRectMake(xOffset, 0, 100, scrollHeight);
   imageView.tag = index;
   Products *product = [self.sliderImages objectAtIndex:index];
   NSLog(@"product name %@",product.name);
   NSString *imageUrl = [NSString stringWithFormat:@"%@",product.imagePath];
}

点击手势识别方法:

- (void) tapGesture:(id)sender 
{ 
    //handle Tap... 
    UIImageView *tmp = (UIImageView *)[sender view]; 
    int tm = (int)(tmp.tag); 
    NSLog(@"self.sliderImages%@",[self.sliderImages objectAtIndex:tm]);
    Products *product = [self.sliderImages objectAtIndex:tm];
    NSLog(@"product%@",product); 
    NSLog(@"product ges %@,, %@",product.name,product.imagePath);
    NSString *imageUrl = [NSString stringWithFormat:@"%@",product.imagePath];
    NSLog(@"full image path :: %@",imageUrl); 
    [self loadFullImage:tm];
}

【问题讨论】:

  • 请贴出你试过的代码
  • 请检查mu code - (void) tapGesture:(id)sender { //handle Tap... UIImageView *tmp = (UIImageView *)[sender view]; int tm = (int)(tmp.tag); NSLog(@"self.sliderImages%@",[self.sliderImages objectAtIndex:tm]);产品 *product = [self.sliderImages objectAtIndex:tm]; NSLog(@"product%@",product); NSLog(@"product ges %@,, %@",product.name,product.imagePath); NSString *imageUrl = [NSString stringWithFormat:@"%@",product.imagePath]; NSLog(@"图片全路径:: %@",imageUrl); [自加载完整图像:tm]; }
  • 请添加标签设置代码。
  • for(int index=0; index
  • @user1591698 请在您的问题中添加您的代码并格式化。这样其他人就可以轻松阅读,并且可以尽快给予。

标签: ios tags nsmutablearray


【解决方案1】:

你确定你没有在标签中得到 nil 吗? (int tm = (int)(tmp.tag))

您如何获得 UIImageView 上的点击?你在使用手势识别器吗?

如果您正在使用手势识别器,请尝试像这样接收触摸和标签

  • (void)touch:(UIGestureRecognizer *)recognizer

【讨论】:

  • 感谢您的回复,我正在获取准确的标签值,并且正在使用手势事件。当我使用此代码进行测试时 -(void)tapGesture:(UIGestureRecognizer *)recognizer ,同样的问题。
  • 其实这行NSLog(@"product%@", product);给出产品对象,但是当我访问 product.name 时它给出 null。
  • 我希望 NSMutable 不返回对象属性,但它包含对象。我将代码更改为 NSMutableArray * productList; productList = [[NSMutableArray alloc] initWithObjects: nil]; for (NSDictionary *productDict in products) { Products *product = [[Products alloc] init]; product.name = [productDict valueForKey:@"name"];[productList addObject:product]; }
  • NSLog(@"self.productList%lu",(unsigned long)productList.count); -> 返回 count Products *product = (Products *)[productList objectAtIndex:index]; -持有对象 NSLog(@"details %@ %@ %@ %@",product,product.name,product.imagePath,product.brand); - 不返回对象属性。
猜你喜欢
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 2014-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多