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 回答