【问题标题】:[__NSArrayM objectAtIndex:]: index 197 beyond bounds [0 .. 8][__NSArrayM objectAtIndex:]: index 197 beyond bounds [0 .. 8]
【发布时间】:2013-12-04 05:06:49
【问题描述】:

我在数据库中有产品表。产品表我有 id 和 product_image 列。每个图像都有 id。我从数据库中获取这些 id 并带到 cat_imageidArray,然后获取图像并带到 productimg_array。将 productimg_array 图像值分配给 imgView1 (UIButton)。我正在使用 [productimg_array objectAtIndex:i]。这里的索引是 0 到 7 等。但是 cat_imageidArray 的值为 183,184,190 等。当我单击 imgView1 按钮时,我收到这些错误 [__NSArrayM objectAtIndex:]:索引 197 超出范围 [0 .. 8]。我明白问题是数组值不匹配。如何将 cat_imageidArray 值分配给 productimg_array 索引。也许那样它会起作用。

const char *sql = "SELECT id,cat_id,product_image,order_by,description FROM product where cat_id = ?";

        NSLog(@"sql is %s",sql);

        sqlite3_stmt *statement;

        if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
            // We "step" through the results - once for each row.

            if (sqlite3_bind_int(statement, 1, categoryId) != SQLITE_OK)
                NSLog(@"%s: bind failed: %s", __FUNCTION__, sqlite3_errmsg(database));

            while (sqlite3_step(statement) == SQLITE_ROW) {

                cat_iamgeId = sqlite3_column_int(statement, 0);
                NSLog(@"cat_iamgeId is %ld",(long)cat_iamgeId);
                [cat_imageidArray addObject:@(cat_iamgeId)];

                product_image = [[NSString alloc] initWithUTF8String:
                                 (const char *) sqlite3_column_text(statement, 2)];
                NSLog(@"product_image is %@",product_image);
                [productimg_array addObject:product_image];                    
            }
        }


for (int i = 0; i<[productimg_array count]; i++ ) {

            NSLog(@"productimg_array_index %@", productimg_array[i]);
            imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];
            Width = Width + 20+(i*74);
            for (int i=0; i<[cat_imageidArray count]; i++){

               NSLog(@"index %@", cat_imageidArray[i]);
               tag=[[cat_imageidArray objectAtIndex:i]intValue];
               imgView1.tag=tag;
               NSLog(@"tag is %d",tag);
            }

            [imgView1 addTarget:self action:@selector(dbsofaClicked:) forControlEvents:UIControlEventTouchUpInside];
            [imgView1 setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:i]]forState:UIControlStateNormal];

           [scrollview addSubview:imgView1];
       }        


imgView1 button click:

-(void)dbsofaClicked:(id)sender{

   NSLog(@"button %d is clicked.", [sender tag]);            
             mmageView=[[UIImageView alloc]initWithFrame:CGRectMake(50,50,150,150)];

    [mmageView setUserInteractionEnabled:YES];

       [mmageView setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:[sender tag]]] placeholderImage:nil options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {

    }];

NSLog productimg_array:

productimg_array (
"http://center.net/projects/View/images/img.png",
"http://center.net/projects/View/images/img1.png",
"http://center.net/projects/View/images/img2.png",
"http://center.net/projects/View/images/img3.png",
"http://center.net/projects/View/images/img4.png",
"http://center.net/projects/View/images/img5.png",
"http://center.net/projects/View/images/img6.png",
"http://center.net/projects/View/images/img7.png",
"http://center.net/projects/View/images/img8.png"
)

当我使用 singleTap 图像时,我没有从数据库值中得到正确的描述。这就是为什么我需要设置 id 而不是 index

- (void)singleTap:(UIGestureRecognizer *)singletap {
  NSLog(@"singleTap1");
    UIView *view = singletap.view;
    switch (view.tag) {
        case 0:

            txtt=[[UITextView alloc]initWithFrame:CGRectMake(50, 100, 200, 100)];

            txtt.text=[descript_array objectAtIndex:0];

            txtt.editable=NO;

            NSLog(@"text is %@",txtt.text);

            [self.view addSubview:txtt];





            break;

        case 1:


            txtt1=[[UITextView alloc]initWithFrame:CGRectMake(50, 100, 200, 100)];

            txtt1.text=[descript_array objectAtIndex:1];

            NSLog(@"text1 is %@",txtt1.text);

            [self.view addSubview:txtt1];



            break;

        case 2:

            txtt2=[[UITextView alloc]initWithFrame:CGRectMake(50, 100, 200, 100)];

            txtt2.text=[descript_array objectAtIndex:2];

            NSLog(@"text2 is %@",txtt2.text);

            [self.view addSubview:txtt2];



            break;

        case 3:

            txtt3=[[UITextView alloc]initWithFrame:CGRectMake(50, 100, 200, 100)];

            txtt3.text=[descript_array objectAtIndex:3];

            NSLog(@"text is %@",txtt3.text);

            [self.view addSubview:txtt3];




            break;

【问题讨论】:

  • db 和 productimg_array 一切正常。只有你的 imgView1 按钮标签比你的 productimg_array 具有更大的价值。
  • 您必须采用包含 Product_id 和 cat_id 的类,而不是采用两个数组。您编写了错误的逻辑并实现了错误的数据结构。所以,首先请解决那个问题。它崩溃是因为你使用了错误的机制。

标签: ios sqlite tags uibutton nsmutablearray


【解决方案1】:

删除解决您问题的内部 for 循环

for (int i = 0; i<[productimg_array count]; i++ ) {

        NSLog(@"productimg_array_index %@", productimg_array[i]);
        imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];
        Width = Width + 20+(i*74);
        //for (int i=0; i<[cat_imageidArray count]; i++){ **This line doesn't need**

           NSLog(@"index %@", cat_imageidArray[i]);
           tag=[[cat_imageidArray objectAtIndex:i]intValue];
           **Changed**//imgView1.tag=tag; 
           imgView1.tag = i;
           NSLog(@"tag is %d",tag);
        //}

        [imgView1 addTarget:self action:@selector(dbsofaClicked:) forControlEvents:UIControlEventTouchUpInside];
        [imgView1 setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:i]]forState:UIControlStateNormal];

       [scrollview addSubview:imgView1];
   }

【讨论】:

  • 查看答案。您在提供标签值时犯了一个错误。
  • 查看数据库表。我有 cat_id 列。 cat_id 有 3、4、52。每个 cat_id 都有 id、product_image 和 description。如果我单击第三个 cat_id,它会显示图像索引 0 到 7。如果我单击 cat_id 4,它会显示 product_image 0 到 3。但问题是当我得到描述时,它会将 0 设置为某个索引值。如果 cat_id 4 值取 cat_id 3 描述值。因为指数值?这就是我与 id (cat_imageidArray) 关联的原因
  • 你能过来聊聊吗?
  • 我已经改变了ans。所以先检查一下。
  • 我不想将索引值 0 设置为某个值。我需要设置 id 值而不是索引。看图片。我有沙发、桌子等类别。沙发是 cat_id 3,桌子是 cat_id 4,依此类推。如果我单击沙发,它会显示图像,然后单击桌子,它会显示图像。但是当我单击沙发时,它的索引值从 0 开始到依此类推,如果我单击表格,它的索引值从 0 开始到依此类推。沙发和桌子的指数值相同。这是我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
相关资源
最近更新 更多