[英]Do I have a memory leak using UIReferenceLibraryViewController?
[英]Why do I have a memory leak in UITableView while interacting and scrolling?
我的tableview工作正常,並且可以毫無問題地加載所有數據。 使用didSelectRowAtIndexPath
並添加/刪除單元格上的復選框時遇到問題。 我將檢查前10個單元格,向下滾動並查看我未輕按的單元格上的復選框。
是什么導致此問題? 下面是我的tableview代碼。
-(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];
}
}
}
}
您需要在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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.