2012-07-26 56 views
2

好吧,我创建了一个单选按钮在每个单元格中的简单表格视图,这是为了了解为什么单元格重复。我把我的行数设置得很高,表明单元格确实重复了。这个简单的项目的目标是在解决这个问题时得出一个合理的结论,因为在这个主题上有几篇文章没有给出正确的结果。当用户在单元格中选择一个按钮时,只有该单元格应该受到影响。这是整个代码。UItableViewCells重复

#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 [email protected]"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]; 
    } 

} 
+0

什么不行? – Mundi 2012-07-26 17:50:46

+1

究竟是什么问题?单元格不会在tableViews中重复使用,您正在重用单元格。 – ohr 2012-07-26 17:57:19

+0

你觉得'dequeueReusableCellWithIdentifier:'中的'Reusable'这个词是什么意思? – 2012-07-26 18:03:55

回答

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; 

如果要更改单元格的内容取决于你应该做的行所以以后你的==零块,

 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; 

希望这有助于..

+0

然而,这会重新加载图像,因此当用户单击按钮图像以更改图像时,一旦向下滚动查看图像,它就会恢复为原始图像 – KING 2012-07-26 22:42:22

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];