0

UITableViewの各行に3つのボタンを表示するという正しい方法を見つけようとしています。しかし、それは繰り返され続け、if(cell == nil)を削除すると繰り返しは停止しますが、一番上に戻ると古い行は空白になります。ここで明らかな何かが欠けていますか?

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    pageNumbers = [models count]/3;
    pageNumbers+=1;
    return pageNumbers;
}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        if(totalRows+1 <= [models count])
        {
            UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn.frame = CGRectMake(0, 1, 104, 160);
            IconEntity* iconForImage = [models objectAtIndex:totalRows];
            [btn setImage:iconForImage.TheImage forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
            [btn setTag:1];

            UILabel* lbl = [[UILabel alloc] init];

            NSString* some_name = iconForImage.TheName;
            lbl.text = some_name;
            lbl.frame = CGRectMake(0, 1, 80, 80);

            [btn addSubview:lbl];
            [cell.contentView addSubview:btn];

            totalRows+=1;
        }

        if(totalRows+1 <= [models count])
        {
            UIButton* btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn2.frame = CGRectMake(108, 1, 104, 160);
            IconEntity* iconForImage = [models objectAtIndex:totalRows];
            [btn2 setImage:iconForImage.TheImage forState:UIControlStateNormal];
            [btn2 addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
            [btn2 setTag:2];


            UILabel* lbl = [[UILabel alloc] init];
            lbl.text = iconForImage.TheName;
            lbl.frame = CGRectMake(0, 1, 80, 80);

            [btn2 addSubview:lbl];
            [cell.contentView addSubview:btn2];
            totalRows+=1;
        }

        if(totalRows+1 <= [models count])
        {
            UIButton* btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn3.frame = CGRectMake(216, 1, 104, 160);
            IconEntity* iconForImage = [models objectAtIndex:totalRows];
            [btn3 setImage:iconForImage.TheImage forState:UIControlStateNormal];
            [btn3 addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
            [btn3 setTag:3];


            UILabel* lbl = [[UILabel alloc] init];
            lbl.text = iconForImage.TheName;
            lbl.frame = CGRectMake(0, 1, 80, 80);

            [btn3 addSubview:lbl];
            [cell.contentView addSubview:btn3];
            totalRows+=1;
        }


    [cell setFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, 440)];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;
}

もう繰り返されませんが、セルが空白になるまでスクロールして戻ることはできません

4

1 に答える 1

0

このようにしてみてください、

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row];

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        if(totalRows+1 <= [models count])
        {
            UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn.frame = CGRectMake(0, 1, 104, 160);
            IconEntity* iconForImage = [models objectAtIndex:totalRows];
            [btn setImage:iconForImage.TheImage forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
            [btn setTag:1];

            UILabel* lbl = [[UILabel alloc] init];

            NSString* some_name = iconForImage.TheName;
            lbl.text = some_name;
            lbl.frame = CGRectMake(0, 1, 80, 80);

            [btn addSubview:lbl];
            [cell.contentView addSubview:btn];

            totalRows+=1;
        }

        if(totalRows+1 <= [models count])
        {
            UIButton* btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn2.frame = CGRectMake(108, 1, 104, 160);
            IconEntity* iconForImage = [models objectAtIndex:totalRows];
            [btn2 setImage:iconForImage.TheImage forState:UIControlStateNormal];
            [btn2 addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
            [btn2 setTag:2];


            UILabel* lbl = [[UILabel alloc] init];
            lbl.text = iconForImage.TheName;
            lbl.frame = CGRectMake(0, 1, 80, 80);

            [btn2 addSubview:lbl];
            [cell.contentView addSubview:btn2];
            totalRows+=1;
        }

        if(totalRows+1 <= [models count])
        {
            UIButton* btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn3.frame = CGRectMake(216, 1, 104, 160);
            IconEntity* iconForImage = [models objectAtIndex:totalRows];
            [btn3 setImage:iconForImage.TheImage forState:UIControlStateNormal];
            [btn3 addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
            [btn3 setTag:3];


            UILabel* lbl = [[UILabel alloc] init];
            lbl.text = iconForImage.TheName;
            lbl.frame = CGRectMake(0, 1, 80, 80);

            [btn3 addSubview:lbl];
            [cell.contentView addSubview:btn3];
            totalRows+=1;
        }


    [cell setFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, 440)];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
    return cell;
}
于 2013-01-10T04:45:35.210 に答える