簡體   English   中英

使用節時如何編輯(即刪除行)uiTableView?

[英]How do you edit(i.e. delete row) uiTableView when using sections?

您好,謝謝大家。 我有一個表格視圖,允許用戶更改為剖視圖。 在標准視圖中一切都很好,但是當用戶切換到剖視圖時,當我嘗試使用(-commitEditingStyle :)函數刪除一行時,我崩潰了。 我在此功能中缺少什么? 分段時應如何刪除行? (我從plist加載我的Table,這是Arrays的字典。每個索引都是tableview中的一行。)

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
     if ([strTitle isEqualToString:@"sectionView"]) {

         @try {


         if (editingStyle == UITableViewCellEditingStyleDelete) {
             // Delete the row from the data source.
           //  NSMutableDictionary *book =[[NSMutableDictionary alloc]init];
             NSMutableDictionary *mynewList = [[NSMutableDictionary alloc]init];
             mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
             NSMutableArray *sectionRow = [mynewList objectForKey:@"Dis"];


             [sectionRow removeObjectAtIndex:indexPath.row];

             [mynewList writeToFile:[self dataFilePath] atomically:YES];

             //
             [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

         } else if (editingStyle == UITableViewCellEditingStyleInsert) {
             // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
         }   
         }
         @catch (NSException *exception) {
             NSLog(@"hello error...%@",exception);
         }



     }

     else {
    /////////////////////// STANDARD VIEW 

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [distribArray removeObjectAtIndex:indexPath.row];
        //
        //        NSMutableArray *contents = [[NSMutableArray alloc] init];
        //        [contents addObject:myMasterList];
        //        //[contents addObject:[NSArray arrayWithObjects:arraydata.text,distroName.text,destorEmail.text, nil]];

        [distribArray writeToFile:[self dataFilePath] atomically:YES];

        //
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
         //////////////////////////////////
     }
}

控制台:-[__ NSCFString removeObjectAtIndex:]:無法識別的選擇器已發送到實例0xe4bd3f0

如您所見,我一直在嘗試格式....不確定下一步該怎么做??? 謝謝。

我的意思是分段的: 在此處輸入圖片說明

因此,您有:

NSMutableDictionary *mynewList = [[NSMutableDictionary alloc]init];
mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

您在其中分配mynewList,然后立即覆蓋它。

但是我認為您可能應該檢查調試器中sectionRow的類型。 我的猜測是它是一個NSString。

在不了解您的數據結構的情況下,很難說出正在發生的事情。

這里有兩個問題。 下一行將產生泄漏,因此不應以這種方式使用。

NSMutableDictionary *mynewList = [[NSMutableDictionary alloc]init];
             mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

除了這個,你可以這樣寫,

NSMutableDictionary *mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

另一件事是

[mynewList objectForKey:@"Dis"]; 並不總是一個數組,它有時以字符串形式出現。 檢查您的數據結構,並確認它是否為數組。

這樣嘗試

id obj = [mynewList objectForKey:@"Dis"];
NSLog(@"%@", [[obj class] description]);

並檢查在NSLog打印的obj的數據類型是什么。 確認它是NSMutableArray而不是NSString

暫無
暫無

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

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