2011-08-04 6 views
0

私は、私のスクロール時にUITableViewの私の行に適用されるというチェックマークがすべて混在する問題があります。私はかなり確かにこれは、iphoneがどのように細胞を再利用しているのかと関係しています。私が離れたところからスクロールするとチェックマークが付いています。Iphone:スクロール時にUITableviewのチェックマークが混乱する

誰かが私にこれを避ける方法や、おそらく私の方法を見て、何かが見えるかどうかについてのヒントを教えてください。

ユーザーが作成した各行の選択を保存してから、どの行が表示されているかを確認して、正しい行にチェックマークが付いているかどうかを確認することもできますが、そうする方法はありません。

ありがとうございます。あなたはセルが状態をデフォルトと行の状態をチェックし、状態を変更するためにそれをリセット取得した後

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    [cell setAccessoryView:nil]; 
} 

NSMutableArray *temp = [[NSMutableArray alloc]init]; 
for (int j = 0; j < [listOfRowersAtPractice count]; j++) { 
    if ([[differentTeams objectAtIndex:indexPath.section] isEqualToString:[[rowersAndInfo objectForKey:[listOfRowersAtPractice objectAtIndex:j]]objectForKey:@"Team"]]) { 
     [temp addObject:[listOfRowersAtPractice objectAtIndex:j]]; 
    } 
} 

[cell.cellText setText:[temp objectAtIndex:indexPath.row]]; 

[temp removeAllObjects]; 
[temp release]; 
// Set up the cell... 


return cell; 
} 


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


     [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     if (cell.accessoryType != UITableViewCellAccessoryCheckmark) { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     }else { 
      cell.accessoryType = UITableViewCellAccessoryNone; 
     } 
    } 

答えて

1

はい選択された行の状態とcellforrowatindexpathで保存します。

EDIT:

あなたはあなたのコード内の名前の一時である、あなたのデータソース内の項目数に等しい項目の数とNSMutabaleArrayを作成することができます。

選択すると、実際には、そのインデックスの値を上記の作成された配列で@ "selected"のようなテキストに変更できます。

cellforrowatindexpathでは、選択または選択解除してからセルのプロパティを変更すると、このテキストを確認できます。そのように選択された状態と選択されていない状態のビットマップ状態を維持しています。

+0

これは、配列が選択されている場合にその配列をどのように配置するかを記述できますが、配列内に既に存在する場合は削除してもかまいませんか? –

+0

私の編集を今すぐチェック –

0

セルを再利用するたびに、セル内のすべての設定をリセット/クリアする必要があります。 だからここに、あなたはセルを取得した直後に、

あなたは

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    [cell setAccessoryView:nil]; 
} 

cell.accessoryType = UITableViewCellAccessoryNone // This and other such calls to clean up the cell 
0

のような何かをする必要があり、このに行く与える:

static NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row]; 

を、私は私のアプリのの1に同じ問題を抱えていました。

チェックマークについては、コアデータストアを使用していますか?

次を使用している場合....

セルは、それを再利用しているので、あなたは、セルのaccessoryTypeをリフレッシュする必要
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
NSManagedObject *item = [[self fetchedResultsController] objectAtIndexPath:indexPath]; 

if ([[item valueForKey:@"checks"] boolValue]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [cell.textLabel setTextColor:[UIColor redColor]]; 
     [cell.detailTextLabel setTextColor:[UIColor redColor]]; 


} else { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    [cell.textLabel setTextColor:[UIColor blackColor]]; 
    [cell.detailTextLabel setTextColor:[UIColor blackColor]]; 
} 


} 

そして......

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
NSManagedObject *selectedObject = [self.fetchedResultsController objectAtIndexPath:indexPath]; 



if ([[selectedObject valueForKey:@"checks"] boolValue]) { 
    [selectedObject setValue:[NSNumber numberWithBool:NO] forKey:@"checks"]; 
} else { 
    [selectedObject setValue:[NSNumber numberWithBool:YES] forKey:@"checks"]; 
} 

[managedObjectContext save:nil]; 

} 
+0

明らかにこれは私のプロジェクトの1つからリッピングされますが、コアデータを使用していない場合は、簡単に値を保存する.plistを作成できます。 – DetartrateD

0

再利用のセルからaccessoryTypeを継承し、これは解決策である:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"cellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    //Refresh acessory for cell when tableview have many cells and reuse identifier 
    if([self.tableView.indexPathsForSelectedRows containsObject:indexPath]){ 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    }else{ 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    cell.textLabel.text = @"Your text cell"; 

    return cell; 
} 
0

それは私のために働いた。.. 、インデックスパスの行のセル内にチェックボックスボタンを作成しました。 everytymのテーブルビューがスクロールされた後、cellForRowAtIndexPathメソッドが 呼び出されるので、私は、セルがオンまたはオフボタンを持っているチェックボックスの選択を定義するための

static NSString *simpleTableIdentifier = @"SimpleTableCell"; 
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 

cell.nameLabel.text = [tableData objectAtIndex:indexPath.row]; 
cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]]; 
cell.prepTimeLabel.text = [prepTime objectAtIndex:indexPath.row]; 
checkbox = [[UIButton alloc]initWithFrame:CGRectMake(290, 5, 20, 20)]; 
[checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox_empty.png"] 
           forState:UIControlStateNormal]; 

[checkbox addTarget:self action:@selector(checkUncheck:) forControlEvents:UIControlEventTouchUpInside]; 
[cell addSubview:checkbox]; 
if(selectedRows.count !=0) 
{ 
if([[selectedRows objectAtIndex:indexPath.row]integerValue]==1) 
{ 
    [checkbox setImage:[UIImage imageNamed: @"checkbox_full.png"] forState:UIControlStateNormal]; 
} 
else 
{ 
    [checkbox setImage:[UIImage imageNamed: @"checkbox_empty.png"] forState:UIControlStateNormal]; 
} 
} 
return cell; 
} 

方法は

- (IBAction)checkUncheck:(id)sender { 
UIButton *tappedButton = (UIButton*)sender; 

NSLog(@"%d",tappedButton.tag); 
if ([[sender superview] isKindOfClass:[UITableViewCell class]]) { 
    UITableViewCell *containerCell = (UITableViewCell *)[sender superview]; 
    NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:containerCell]; 
    int cellIndex = cellIndexPath.row; 
    NSLog(@"cell index%d",cellIndex); 
    [selectedRows insertObject:[NSNumber numberWithInt:1] atIndex:cellIndex]; 

} 
NSLog(@"%@",selectedRows); 
if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"checkbox_empty.png"]]) 
{ 
    [sender setImage:[UIImage imageNamed: @"checkbox_full.png"] forState:UIControlStateNormal]; 
} 
else 
{ 
    [sender setImage:[UIImage imageNamed: @"checkbox_empty.png"] forState:UIControlStateNormal]; 

} 
} 
ようであるかどうかを確認するためにcellForRowAtIndexPathに条件を追加する必要がありました

selectedRows配列を初期化することを忘れないでください。 ハッピーコーディング... !!!

関連する問題