2016-03-21 10 views
0

私はカスタムセルを持つTableViewを持っています。選択されたラジオボタン(画像)が選択されて表示されます。すべて正常に動作しますが、スクロールを開始するとセルが正しく表示されません(選択されていないラジオボタン(画像)は「選択された」セルに配置されます)。誰もがこれを修正する方法を知っていますか?UITableviewのスクロールで画像を変更する、UITableviewスクロールする問題

私のコードでは、事前に

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

    ProfileTableViewCell *cell = (ProfileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
NSString *tempTitle = [tempArr objectAtIndex:indexPath.row]; 
      [cell.titleLblOutlet setText:tempTitle]; 
      cell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@"radio_inactive.png"]; 
      if (indexPath == self.selectedCellIndexPath) { 
       cell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@"radio_active.png"]; 
      } 
    return cell; 
} 

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    /* 
    This works nicely but the only problem I found with this approach is that the user can deselect a row by tapping on it, leaving nothing selected. Simply add the following to prevent this: -(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath { return nil; } 
    */ 
    for (NSIndexPath* selectedIndexPath in tableView.indexPathsForSelectedRows) { 
     if (selectedIndexPath.section == indexPath.section) { 
      [tableView deselectRowAtIndexPath:selectedIndexPath animated:NO] ; 

      ProfileTableViewCell *cell = [tableView cellForRowAtIndexPath:selectedIndexPath]; 
      cell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@"radio_inactive.png"]; 

     } 

    } 
    return indexPath ; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

      if (indexPath != self.selectedCellIndexPath) { 
       ProfileTableViewCell *tempPreviousCell = [tableView cellForRowAtIndexPath:self.selectedCellIndexPath]; 
       tempPreviousCell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@"radio_inactive.png"]; 

       self.selectedCellIndexPath = [tableView indexPathForSelectedRow]; 

       ProfileTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
       cell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@"radio_active.png"]; 
      } 
} 

感謝を見てください。

+0

あなたはセル再利用の問題があり、それは何百回も(不正確な)答えられています。 – Avi

+0

cellForRowAtIndexPathに他の条件がありません。 if(indexPath == self.selectedCellIndexPath){ cell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@ "radio_active.png"];これを試してください。 } else {cell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@ "radio_inactive.png"]; } –

+0

@Avi [tableView dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath];これは私が使ったコードです。私の質問が何百万回も返答されたのかどうか教えてください。 – Lion

答えて

2

if条件をcellForRowAtIndexPathパスに変更します。

if (indexPath.row == self.selectedCellIndexPath.row && 
indexPath.section == self.selectedCellIndexPath.section) 
+0

ありがとうたくさんの先生 – Lion

関連する問題