我怎么能删除我的表格的单元格数组

时间:2011-11-21 13:13:28

标签: ios uitableview

我从表中删除行时遇到问题。我有一个表的单元格和一个SQL数据库的类。然后我从数据库加载表并用它做smth。现在当我尝试通过索引路径删除行时,我有一个例外。

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        databasecontroller=((SexyGirlsWeatherAppDelegate *)[[UIApplication sharedApplication] delegate]).databasecontroller;
        self.favorites=[databasecontroller select:@"SELECT Name, id FROM Favorites"];
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        cell = (CustomCell *)[favoritesTableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        [favoritesTableView setBackgroundColor:[UIColor clearColor]];
        cell.name.text = [[favorites objectAtIndex:indexPath.row] objectForKey:@"Name"];
        cell.currenttemp.text=@"+45 C";
        cell.currentweather.image=[UIImage imageNamed:@"sunny"];
        //[cell.selectcell setBackgroundImage:[UIImage imageNamed:@"selectfalse"] forState:UIControlStateNormal];
        cell.razdelitel.image=[UIImage imageNamed:@"delenie"];
        if(editModeOn==YES) [cell EditModeON];
        return cell;
    }


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 if(editModeOn==YES)
 {
    int existcount=0;
    for(int z=0; z<[selectrowsarray count];z++)
    {
        if([selectrowsarray objectAtIndex:z]==indexPath) // check for exist of item
        {
            [selectrowsarray removeObjectAtIndex:z];
            existcount++;
        }

    }
    if(existcount==0) 
    {
        [selectrowsarray addObject:indexPath];
    }
 }
}
-(IBAction) deleteSelectedTownsFromFavorites:(id)sender // here i try to delete cells
{
   // [favoritesTableView beginUpdates];
   // [databasecontroller exec:[NSString stringWithFormat:@" DELETE FROM Favorites WHERE Id=%d", 3]];
 //   [favoritesTableView reloadData];
  //  [databasecontroller exec:@"UPDATE Favorites"];
//    for(int t=0; t<[selectrowsarray count];t++)
//    {
//
    [favorites removeLastObject];
        [favoritesTableView deleteRowsAtIndexPaths:selectrowsarray  withRowAnimation:UITableViewRowAnimationMiddle];
//    }
    optionsview.hidden=true;
  //  [favoritesTableView endUpdates];

} 


- (NSInteger)tableView:(UITableView *)favoritesTableView numberOfRowsInSection:(NSInteger)section
{
    //return [favorites count];
    return [favorites count];
}

如何同步2个数组(来自数据库和selectcelladdresses)以纠正删除行?

1 个答案:

答案 0 :(得分:0)

您还必须从数据源中删除单元格。