2016-04-15 5 views
0

私のtableviewは正常に動作しており、問題なくすべてのデータをロードしています。 didSelectRowAtIndexPathを使用して問題が発生し、セルのチェックボックスを追加/削除します。私は最初の10のセルをチェックし、スクロールして、タップしなかったセルのチェックボックスを表示します。なぜ私はインタラクションとスクロール中にUITableViewでメモリリークが発生するのですか?

この問題の原因は何ですか?以下は私のテーブルビューのコードです。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *cellIdentifier = @"reuseCellForFilter"; 
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
Category *category = [self.categories objectAtIndex:indexPath.row]; 
cell.textLabel.text = category.title; 

return cell; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return self.categories.count; 
} 

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


NSLog(@"%@", indexPath); 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
Category *category = [self.categories objectAtIndex:indexPath.row]; 
if(cell.accessoryType == UITableViewCellAccessoryNone) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    [self.titles addObject:category]; 
} 
else { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    for (Category *categoryID in [self.titles reverseObjectEnumerator]) { 
     if (categoryID.categoryID == category.categoryID) { 
      [self.titles removeObject:categoryID]; 
     } 
    } 
} 
} 
+0

(categoryID.categoryID == category.categoryID)がポインタ型の変数に対して正しく動作するかどうかを確認してください。 – heximal

答えて

2

あなたはdidSelectRowAtIndexPath方法でチェック/未チェック状態を保存し、その後cellForRowAtIndexPath方法でそれを復元する必要があります。

例えば:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellIdentifier = @"reuseCellForFilter"; 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
    cell.selectionStyle = [self.selected containsObject:category.categoryID] ? UITableViewCellSelectionStyleCheckmark : UITableViewCellSelectionStyleNone; 
    Category *category = [self.categories objectAtIndex:indexPath.row]; 
    cell.textLabel.text = category.title; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Category *category = [self.categories objectAtIndex:indexPath.row]; 
    if ([self.selected containsObject:category.categoryID]) { 
     [self.selected removeObject:category.categoryID]; 
     self.tableView reloadRowsAtIndexPaths:@[indexPath] withAnimation: UITableViewRowAnimationNone]; 
    } 
} 

ます。またdidSelectRowAtIndexPath方法で問題を抱えています。カテゴリのマーキングをオンにして、このカテゴリを配列に追加します。カテゴリからチェックを外すには、配列からcategoryIDを削除します。

[self.titles addObject:category]; 
... 
[self.titles removeObject:categoryID]; 
関連する問題