无法删除表中的右侧单元格

时间:2011-11-09 08:20:01

标签: objective-c ios uitableview

我正在制作一款小应用。现在麻烦了。删除表格自定义单元格时出现问题。

它继续删除顶部单元格而不是正确选择的单元格。我删除了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;
}

1 个答案:

答案 0 :(得分: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];
    }
}