-1

我想通过添加一个按钮来插入和删除数据,当我单击我的按钮查询有效但数据没有立即插入或删除我必须重新启动应用程序才能在我的 tableView 中查看插入或删除的行,我还添加了 tableView 重新加载数据,但它在这里不起作用是这个视图中的一些和平代码我想插入或删除行

- (void)viewDidLoad
{
    [super viewDidLoad];
    isLoadingAlphabets=1;

    DBHandler *obj= [[DBHandler alloc]init];
    Array = [obj loadFavoriteWords];

    isSearching = 0;
    displayItems = [[NSMutableArray alloc] initWithArray:Array];    
    // Do any additional setup after loading the view, typically from a nib.
      [tableview reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
    - (void)dealloc {
        [tableview release];
        [super dealloc];
    }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(isSearching){
        return [displayItems count];   
    }
    else {
        NSLog(@"Count: %d", [Array count]);
        return [Array count];
    }
}

这是我在收藏夹中添加或删除行的按钮

- (void)viewDidLoad
{
     [super viewDidLoad];

    DBHandler* db=[[DBHandler alloc]init];
   // NSLog(@"asas%@",detail_word);
    if(![db isAlreadyInFavorites:word_id])
         {
            [btnFavorite setImage:[UIImage imageNamed: @"star-grey.png"  ] forState:UIControlStateNormal];
        } else {
            [btnFavorite setImage:[UIImage imageNamed:@"star-yellow.png"] forState:UIControlStateNormal ];  
        }
    // Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)btn:(id)sender {
    DBHandler *db = [[DBHandler alloc]init];

    if(![db isAlreadyInFavorites:word_id]){

        [btnFavorite setImage:[UIImage imageNamed:@"star-yellow.png"] forState:UIControlStateNormal];
        [db addFavoriteWord:word_id];

    }
    else{

        [db deleteFavoriteWord:word_id];

        [btnFavorite setImage:[UIImage imageNamed:@"star-grey.png"] forState:UIControlStateNormal];

    } 
}
4

3 回答 3

2

在您的 Button 操作中重新加载表格视图。

      - (IBAction)btn:(id)sender {
            DBHandler *db = [[DBHandler alloc]init];

            if(![db isAlreadyInFavorites:word_id]){

                [btnFavorite setImage:[UIImage imageNamed:@"star-yellow.png"] forState:UIControlStateNormal];
                [db addFavoriteWord:word_id];

            }
            else{

                [db deleteFavoriteWord:word_id];

                [btnFavorite setImage:[UIImage imageNamed:@"star-grey.png"] forState:UIControlStateNormal];

            } 

// Upadate Your Array Here

          yourarray=[db getfavdata ];

           [tableview reloadData];

        }




if your table is in another view then reload the tableview in `viewwillAppear` method.



     -(void)viewWillAppear:(BOOL)animated
        { 
 DBHandler *db = [[DBHandler alloc]init];
     yourarray=[db getfavdata ];
        [tableview reloadData];

        }
于 2012-11-16T13:11:58.117 回答
1

只需从您的 displayItems 或 Array 中删除所有数据,然后从数据库中再次添加这些新数据,如波纹管

- (IBAction)btn:(id)sender {
    DBHandler *db = [[DBHandler alloc]init];

    if(![db isAlreadyInFavorites:word_id]){

        [btnFavorite setImage:[UIImage imageNamed:@"star-yellow.png"] forState:UIControlStateNormal];
        [db addFavoriteWord:word_id];

    }
    else{

        [db deleteFavoriteWord:word_id];

        [btnFavorite setImage:[UIImage imageNamed:@"star-grey.png"] forState:UIControlStateNormal];

    } 
    Array = [db loadFavoriteWords];
    if([displayItems count]>0){
          [displayItems removeAllObjects];
    }
    displayItems = [[NSMutableArray alloc] initWithArray:Array];    
    [tableview reloadData];
}
于 2012-11-16T13:14:24.253 回答
0
You should update your tableview datasource array after adding or deleting any object. In your case you are not resetting your Array in - (IBAction)btn:(id)sender method. Try below code-

- (IBAction)btn:(id)sender {
        DBHandler *db = [[DBHandler alloc]init];

        if(![db isAlreadyInFavorites:word_id]){

            [btnFavorite setImage:[UIImage imageNamed:@"star-yellow.png"] forState:UIControlStateNormal];
            [db addFavoriteWord:word_id];

        }
        else{

            [db deleteFavoriteWord:word_id];

            [btnFavorite setImage:[UIImage imageNamed:@"star-grey.png"] forState:UIControlStateNormal];

        } 
       Array = [obj loadFavoriteWords];
       [tableview reloadData];
    }
于 2012-11-16T13:14:02.777 回答