我想在uitableview
按下按钮时重新加载uicellview
。为了清楚起见,我正在做的是,我在cellTableView上创建了一个收藏夹按钮,当我第一次按下它时,它变亮,星星点亮,当我再次按下它时它会关闭,星光变为关闭,我正在保存on& sqlite中的off值(当它是1 - > favourited或on,当它是0 - >不受欢迎或关闭时)。
我的问题是,当我按下星号按钮时,sqlite中的值正在变化但是星形图像没有变化。
这是我的代码..
- (void)viewDidLoad
{
[super viewDidLoad];
[self retrieveDataFromSqlite:1];
}
-(sqlite3 *) openDataBase{
sqlite3 *database;
NSString* docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString* dbPath = [docPath stringByAppendingPathComponent:@"HealthyTips.sqlite"];
NSLog(@"dbPath: %@",dbPath);
if (sqlite3_open([dbPath UTF8String], &database) != SQLITE_OK) {
NSLog(@"Failed to open database!");
}else{
NSLog(@"database opened!");
}
return database;
}
-(void) retrieveDataFromSqlite:(int)num {
tipsArray = [[NSMutableArray alloc] init];
sqlite3 *myDatabase = [self openDataBase];
const char *sqlSelect = "select * from tips_table";
if (num == 1) {
// sqlSelect = "select * from tips_table";
} else if (num == 2){
}else{
}
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(myDatabase, sqlSelect, -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
int tips_id = sqlite3_column_int(compiledStatement, 0);
NSString *tips_content = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 1)];
NSString *tips_favorite = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 2)];
Tips *tips = [[Tips alloc] initWithUniqueId:tips_id tips_content:tips_content tips_favorite:tips_favorite];
[tipsArray addObject:tips];
} // while end
}// prepare end
sqlite3_finalize(compiledStatement);
sqlite3_close(myDatabase);
}
-(void) updateFavoriteTip:(NSString *)tipFavorite tipID:(int)tipID {
sqlite3 *myDatabase = [self openDataBase];
sqlite3_exec(myDatabase, [[NSString stringWithFormat:@"UPDATE tips_table set tip_favorites = '%@' WHERE _id = '%d' ", tipFavorite, tipID] UTF8String], NULL, NULL, NULL);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [tipsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"tipCell";
TipsTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[TipsTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Tips *temp= (Tips *)[self.tipsArray objectAtIndex:indexPath.row];
[cell.tipsContent setText:[NSString stringWithFormat:@"%@",temp.tips_content]];
[cell.tipsContent setFont:[UIFont fontWithName:@"Bauhaus Md BT" size:20]];
if ([temp.tips_favorite isEqualToString:@"1"]) {
[cell.favoriteImage setImage:[UIImage imageNamed:@"tips_star_on.png"] forState:UIControlStateNormal];
}else{
[cell.favoriteImage setImage:[UIImage imageNamed:@"tips_star_off.png"] forState:UIControlStateNormal];
}
[cell.favoriteImage addTarget:self action:@selector(setFavoriteTip:)
forControlEvents:UIControlEventTouchUpInside];
cell.favoriteImage.tag = indexPath.row;
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:@"pattern_gray0.png"];
cell.backgroundView = av;
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background_color.png"]];
return cell;
}
-(IBAction)setFavoriteTip:(UIButton *)sender{
int tag = sender.tag;
Tips *temp= (Tips *)[self.tipsArray objectAtIndex:tag];
NSLog(@"in setFavoriteTip, favorite: %@ - id: %d",temp.tips_favorite, temp.tips_id);
if ([temp.tips_favorite isEqualToString:@"1"]) {
[self updateFavoriteTip:@"0" tipID:temp.tips_id];
}else{
[self updateFavoriteTip:@"1" tipID:temp.tips_id];
}
// reloads the table well but the image of star not changing.
[self.tableView reloadData];
}
希望有人能帮助我。