-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

ボタン操作内でテーブルビューをリロードします。

      - (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 に答える