簡體   English   中英

刪除最后一個單元格后未選擇UITableView單元格

[英]UITableView cells are not selected after deleting last cell

我將UITableView分為三個部分:三個部分中的兩個是常數(每個常數只有一個不變的單元格),第一部分是您可以在單元格中添加或刪除的部分。 除了從該部分刪除單元格外,其他所有功能都可以正常運行:一旦刪除了最后一個單元格,此單元格便消失了(如預期的那樣),但其他部分則無法正常工作,這意味着我無法單擊它們(它們均加載另一個視圖)。 如果我在第一部分為空時加載應用程序,那么一切都很好。 僅在刪除此部分的最后一個單元格后,此事件才會發生。 我試圖將日志放置在代碼中的任何地方,並且一切正常。

將不勝感激任何建議。

這是一些可能相關的代碼。 如果需要,樂意提供更多:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (indexPath.section == 0) {
    return YES;
 } else {
    return NO;
 }
}


- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

[tblSimpleTable beginUpdates];

if (editingStyle == UITableViewCellEditingStyleDelete) {

     NSMutableArray * section = [listOfItems objectAtIndex:indexPath.section];    
    [section removeObjectAtIndex:indexPath.row];

    //UPDATING USERS DEFAULTS
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];            
    [defaults setObject:section forKey:@"listOfAccessNumbers"];

    [tblSimpleTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    [tblSimpleTable reloadData];


    }


}     
else if (editingStyle == UITableViewCellEditingStyleInsert) {

...

}

[tblSimpleTable endUpdates];

}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

NSLog(@"****************** entering setEditing");

[super setEditing:editing animated:animated];
[tblSimpleTable setEditing:editing animated:YES];
if (editing) {
     editButton.enabled = NO;
} else {
    editButton.enabled = YES;
}
}

更新:添加更多方法:

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

NSLog(@"****************** entering cellForRowAtIndexPath");

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; //try here diff styles
}


NSArray *array = [[NSArray alloc] init];
array = [listOfItems objectAtIndex:indexPath.section];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.textColor = [UIColor colorWithRed:.196 green:0.3098 blue:0.52 alpha:1.0];

if (sectionIndicator == YES){

    if (indexPath.section == 0) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else if (indexPath.section == 1) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

}
else
{
    if (indexPath.section == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if (indexPath.section == 1) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
}


return cell;
}




 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return [listOfItems count];    
 }



 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [[listOfItems objectAtIndex:section] count];
 }

不要在beginUpdatesendUpdates內部調用reloadData 您始終可以在beginUpdates內部將布爾標志設置為true,並在調用endUpdates之后檢查該標志,以查看是否應調用reloadData ,盡管我不確定為什么需要調用reloadData

我想我可能知道發生了什么,所以我將其發布為答案。

我只是注意到您正在調用listOfItems以獲得從中刪除項目的臨時數組。 我還看到您正在更新NSUserDefaults的對象。 完成此操作后, listOfItems也會更新? 如果不是,那就是您的問題所在。 它將繼續保存與以前相同的所有信息,因此看起來好像沒有更新發生。 我希望情況確實如此,因為這將使您的修復非常容易。 如果不是這樣,可能有必要查看您的dataSource方法以查看問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM