此代码中的两个错误。在多次删除行的UITableView删除行中的错误
第一次当我删除行tableview删除最后一行未选中的行。
第二个错误是当试图删除多个行时,将删除成功,但当重新加载tableview只有一行删除。休息行会再次出现。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return result.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath : indexPath];
if (!(cell == nil)) {
name = (UILabel *)[cell viewWithTag:10];
name.text = [result objectAtIndex : indexPath.row];
}
name.tag=indexPath.row;
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (IBAction)action:(id)sender {
[_myTableView setEditing:YES animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath : indexPath];
if (!(cell == nil)) {
name = (UILabel *)[cell viewWithTag:10];
name.text = [result objectAtIndex : indexPath.row];
}
name.tag=indexPath.row;
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) {
[result removeObjectAtIndex:indexPath.row];
[tableView reloadData];
[spinner startAnimating];
NSString *id = [profile objectForKey:@"user_id"];
NSString *tb =tid;
NSString *string = [NSString stringWithFormat:@"userid=%@&topiid=%@",id,tb];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"myDeleteUrl"]];
NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setHTTPMethod:@"POST"];
[request setHTTPBody : data];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration
defaultSessionConfiguration]]; [[session dataTaskWithRequest : request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *JSON= [NSJSONSerialization JSONObjectWithData :data options :NSJSONReadingMutableContainers error: nil];
NSArray *alertArray = [JSON objectForKey:@"deletebookmark"];
for (NSDictionary *alert in alertArray){
if ([[alert objectForKey:@"status"] isEqualToString:@"done"]) {
NSLog(@"done");
[spinner stopAnimating];
}
else{
NSLog(@"notDone");
[spinner stopAnimating];
}
}
}]resume];
}
}
编辑你的问题得当,使其更具可读性。 – Hasya
我是新的在stackoverflow我不知道该怎么做。谢谢你.. –
请参考[如何问一个好问题?](// stackoverflow.com/help/how-to-ask)和[如何创建一个最小,完整和可验证的示例](// stackoverflow.com/help/mcve)。 – Olaia