我想通过添加按钮来插入和删除数据,当我点击我的按钮查询工作但数据没有立即插入或删除我必须重新启动应用程序以查看我的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];
}
}
答案 0 :(得分:2)
重新加载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];
}
答案 1 :(得分:1)
只需从displayItems或Array中删除所有数据,然后再添加数据库中的这些新数据,如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];
}
答案 2 :(得分: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];
}