2012-07-26 48 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];