2012-11-16 129 views
-1

我想通過添加一個按鈕插入和刪除數據,當我點擊我的按鈕查詢的作品,但數據一次不插入或刪除我必須重新啓動應用程序來查看插入或刪除的行在我的tableView,我還添加了的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]; 
    } 
} 

這裏的代碼 一些和平是從哪兒IM添加或刪除行我的按鈕在收藏夾

- (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]; 

    } 
} 

回答

1

只是從你的displayItems或從陣列中刪除所有數據,並從數據庫中再次添加這個新的數據,如belllow

- (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]; 
} 
+0

是這將是btn? –

+0

是在btn行動事件的隊友只是使用此代碼,請參閱我在你的btn行動事件中添加一些行.. –

2

你的內心按鈕actioon重新加載tableview。

 - (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]; 

     } 
+0

我這樣做,但沒有工作:( –

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]; 
    } 
相關問題