2011-11-09 62 views
0

我正在製作一個小應用程序。現在有麻煩了。通過刪除表格定製的單元格來解決問無法刪除表格中的右側單元格

它不斷刪除頂部單元格而不是右側選定的單元格。我刪除單元號20,它仍然刪除單元號1.我不知道爲什麼。請幫助我。萬分感激。

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

{ 
    [sortedArray removeObjectAtIndex:indexPath.row]; 
    [self.tableView reloadData]; 
} 

,這裏是數組:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    if (detail == nil) { 
     detail = [[UrlDetail alloc] init]; 
    } 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; 

    self.arrayData = [NSMutableArray arrayWithContentsOfFile:path]; 

    NSMutableArray *filterArr = [self filterArray]; 

    sortedArray = [[NSMutableArray alloc] initWithArray:filterArr copyItems:YES]; 

    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] 
               initWithBarButtonSystemItem:UIBarButtonSystemItemAdd                          
               target:self 
               action:@selector(actionAddNewURL:)] autorelease]; 

} 

,這裏是過濾函數:

-(NSMutableArray *)filterArray 
{ 

NSMutableArray *filterArr = [[NSMutableArray alloc] init]; 
NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 


[filterArr addObject:tempArray]; 
[tempArray release]; 

for (NSDictionary *item in arrayData) { 
    if ([tempArray count]==0) 
    { 
     [tempArray addObject:item]; 
    } 
    else 
    { 
     NSDictionary *anItem = [tempArray objectAtIndex:0]; 
     NSString *first = [[anItem objectForKey:@"url"] substringToIndex:1]; 
     NSString *last = [[item objectForKey:@"url"] substringToIndex:1]; 

     if ([first isEqualToString:last]) 
     { 
      [tempArray addObject:item]; 
     } else 
     { 
      tempArray = [[NSMutableArray alloc] init]; 
      [tempArray addObject:item]; 
      [filterArr addObject:tempArray]; 
      [tempArray release]; 
     } 
    } 
} 
// NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:filterArr copyItems:YES]; 
return filterArr; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if([sortedArray count]>0){ 

     NSLog(@"number of section: %d", [sortedArray count]); 
     return [sortedArray count]; 
    } 
    return 0; 
} 


- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section 
{ 
    if (isTab) { 
     if ([self.sortedArray count] > section) { 
      NSDictionary *dictionary = [[sortedArray objectAtIndex:section] objectAtIndex:0]; 
      NSString *string = [dictionary objectForKey:@"url"];    
      return [NSString stringWithFormat:@"%@", [[string substringToIndex:1] uppercaseString]]; 
     } 
     else return nil; 
    } else 
    { 
     return nil; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    CustomCell *cell = (CustomCell *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     cell = myOwnCell; 
    } 

    NSDictionary *dataItem = [[sortedArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 

    cell.urlName.text = [dataItem objectForKey:@"url"]; 
    cell.titleLabel.text = [dataItem objectForKey:@"title"]; 
    cell.urlName.font = [UIFont italicSystemFontOfSize:14.0]; 
    cell.imageIcon.image = [UIImage imageNamed:[dataItem objectForKey:@"image"]]; 

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    // Configure the cell. 
    return cell; 
} 
+0

什麼陣列你的表的數據源? – tobiasbayer

回答

0

嘗試,如果它的工作原理是一個:

- (void) tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     NSString *key = [[sortedArray allKeys] objectAtIndex:indexPath.row]; 

     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop]; 
     [sortedArray removeObjectForKey:key]; 
    } 
} 
+0

My sortedArray是NSMutableArray,它不響應allKeys。你有什麼主意嗎?謝謝。 – user1035877

+0

如果您未在陣列中設置按鍵,則可以忽略allKeys – denil