0

我在 gridview 上显示图像。如果我有 4、8、12、16 等图像,它可以正常工作,但是当我的图像或多或少 4、8、12 时,我无法点击它们。如果我有 6 张图片,那么我可以点击 4 张图片,但我无法点击第 5 和第 6 张图片。

这是我的表格视图代码

- (UITableViewCell *)tableView:(UITableView *)tableView_p cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *hlCellID = @"hlCellID";

    UITableViewCell *hlcell = [tableView_p dequeueReusableCellWithIdentifier:hlCellID];
    if(hlcell == nil) {
        hlcell =  [[[UITableViewCell alloc] 
                    initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hlCellID] autorelease];
        hlcell.accessoryType = UITableViewCellAccessoryNone;
        hlcell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    int section = indexPath.section;
    NSMutableArray *sectionItems = [sections objectAtIndex:section];

    int n = [sectionItems count];
    int i=0,i1=0; 

    while(i<n){
        int yy = 4 +i1*74;
        int j=0;
        for(j=0; j<4;j++){

            if (i>=n) break;
            NSLog(@"Counter i %d , j %d , n %d ",i,j, n);
            Item *item = [sectionItems objectAtIndex:i];

            CGRect rect = CGRectMake(16+75*j, yy, 65, 65);
            UIButton *button=[[UIButton alloc] initWithFrame:rect];
            [button setFrame:rect];
            //UIImage *buttonImageNormal=[UIImage imageNamed:item.image];
            UIImage *buttonImageNormal = item.savedImage;
            [button setBackgroundImage:buttonImageNormal    forState:UIControlStateNormal];
            [button setContentMode:UIViewContentModeCenter];

            NSString *tagValue = [NSString stringWithFormat:@"%d%d", indexPath.section+1, i];
            button.tag = [tagValue intValue];
            //NSLog(@"....tag....%d", button.tag);

            [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [hlcell.contentView addSubview:button];
            [button release];

            /*UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake((75*j)-4, yy+44, 80, 12)] autorelease];
             label.text = item.title;
             label.textColor = [UIColor blackColor];
             label.backgroundColor = [UIColor clearColor];
             label.textAlignment = UITextAlignmentCenter;
             label.font = [UIFont fontWithName:@"ArialMT" size:12]; 
             [hlcell.contentView addSubview:label];
             */
            i++;
        }
        i1 = i1+1;
    }
    return hlcell;
}

在此处输入图像描述

- (void)loadView {

    [super loadView];
    sections = [[NSMutableArray alloc] init];
    imagesPath = [[NSMutableArray alloc] init];

    for(int s=0;s<1;s++) { // 4 sections
        sectionPattern = [[NSMutableArray alloc] init];


        for(int i=0;i<9+3;i++) {  // 12 items in each section 
            NSLog(@"Loop First %d",i);
           //Here i am allocating images to sectionPattern and than in sections and using it later in above code
            NSLog(@"Loop OUt %d",i);
        }
        [sections addObject:sectionPattern];

    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
4

1 回答 1

1

如果没有更多代码,很难说,但是您应该确保返回正确的值- (NSInteger)numberOfRowsInSection:(NSInteger)section

于 2012-11-28T21:43:49.517 回答