не могу удалить нужную ячейку в таблице - PullRequest
0 голосов
/ 09 ноября 2011

Я делаю небольшое приложение.И возникли проблемы прямо сейчас.Проблема, удаляя настроенную ячейку таблицы.

это продолжает удалять верхнюю ячейку вместо правой выбранной ячейки.Я удаляю ячейку № 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 голосов
/ 09 ноября 2011

Попробуйте, если он работает:

- (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];
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...