2

好的,我在每个单元格中创建了一个带有单选按钮的简单表格视图,这样做是为了查看单元格重复的原因。我将我的行数设置为高得离谱,以表明细胞确实重复。这个简单项目的目标是在解决这个问题时得出一个合理的结论,因为有几个关于这个主题的帖子都没有给出正确的结果。当用户在单元格中选择一个按钮时,该单元格并且只有该单元格应该受到影响。这是完整的代码。

    #import "faQViewController.h"

     @interface faQViewController ()

    @end

    @implementation faQViewController
    @synthesize button1,button2;

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    }

     - (void)viewDidUnload
    {
     [super viewDidUnload];
     // Release any retained subviews of the main view.
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 30;


    }


    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath{
    static NSString *cellIdentifier =@"cell";
    button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    button1.frame = CGRectMake(0, 0, 22, 32);
    [button1 setImage:[UIImage imageNamed:@"radioOff.png"] forState:UIControlStateNormal];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell ==nil) {        
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier
                 ] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
         [cell.contentView
          addSubview:button1];
    }

   // cell.imageView.image = [UIImage imageNamed:@"radioOff.png"];


    return cell;
}

    -(IBAction)buttonPressed:(id)sender{
    if ([sender imageForState:UIControlStateNormal ]== [UIImage imageNamed:@"radioOff.png"]){
    [sender setImage:[UIImage imageNamed:@"radioOn"] forState:UIControlStateNormal];
    }else {

        [sender setImage:[UIImage imageNamed:@"radioOff.png"] forState:UIControlStateNormal];
    }

}
4

2 回答 2

1

丹尼尔斯的回答帮助我清理了一些东西来解决我的问题。我的问题是我看到sections 0,1,2正在被独特地创建,但随后second 3被生成相同section 0,这是我的设置:

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

    CustomCell* cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    [cell.textLabel setText:[NSString stringWithFormat:@"SECTION %d", indexPath.section]];

    NSLog(@"SECTION %d", indexPath.section);

    return cell;
}

我需要更改的是cellIndentifier因为我将它们全部设置为相同的 ID:

NSString *cellIdentifier = [NSString stringWithFormat:@"cell-%d", indexPath.section];
于 2013-07-17T15:50:59.450 回答
1

您正在重用单元格,因此如果您不更改内容,您会看到其他行出现相同的单元格。由于您只在分配单元格时设置内容,因此在重复使用单元格时内容将保持不变

所以

  //Here you tell the tableView to re use a cell if one is available for reuse
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
   //if the cell is nil (none available for reuse)
    if (cell ==nil) {        
        //you create the cell and set its content
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier
                 ] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
         [cell.contentView
          addSubview:button1];
    }
    //return the cell
   return cell;

如果您想根据行更改单元格内容,您应该在 cell==nil 块之后这样做

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
       //if the cell is nil (none available for reuse)
        if (cell ==nil) {        
            //you create the cell and set its content
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier
                     ] autorelease];
            }

          //set the content
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
             [cell.contentView
              addSubview:button1];

        //return the cell
       return cell;

希望这可以帮助..

于 2012-07-26T18:01:31.170 回答