jeudi 22 janvier 2015

How to reload TableVIew Data in iOS?

i am newbie in iOS development and new in to sqllite i know this question asked many times but i not get solution. i worked on sqllite app in my app one view contain sqllite table data i fetch data from data table like as



-(void)getTextFromDb
{
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];

_databasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
@"sampleDatabase.db"]];

const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
int BookMark=1;
if (sqlite3_open(dbpath, &_myDatabase) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"SELECT PostId,PostTitle,ImageLink,ShortDescription,Category,PostDate FROM ALLREADTABLE WHERE Bookmark=%d ORDER BY ID DESC " ,BookMark];

const char *query_stmt = [querySQL UTF8String];

if (sqlite3_prepare_v2(_myDatabase, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
[self.postId removeAllObjects];
[self.postTitle removeAllObjects];
[self.imageLink removeAllObjects];
[self.shortDecsription removeAllObjects];
[self.category removeAllObjects];
[self.postDate removeAllObjects];
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString *postId = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 0)];
NSString *postTitle = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 1)];
NSString *ImageLink = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 2)];
shortDescription = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 3)];
NSString *Category = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 4)];
NSString *postDate = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 5)];
[self.postId addObject:[NSString stringWithFormat:@" %@ ", postId]];
[self.postTitle addObject:[NSString stringWithFormat:@"%@ ",postTitle]];
[self.imageLink addObject:[NSString stringWithFormat:@"%@",ImageLink]];
[self.shortDecsription addObject:[NSString stringWithFormat:@" %@",shortDescription]];
[self.category addObject:[NSString stringWithFormat:@" %@",Category]];
[self.postDate addObject:[NSString stringWithFormat:@" %@",postDate]];
}
sqlite3_finalize(statement);
}
sqlite3_close(_myDatabase);
[self.bookMarkTable reloadData];
}

}


And display it in to TableView when my TableView Cell Selected then i pass Data in to my WebView. here WebView Contain BoookMark Remove Butoon.


In Bookmark Remove Button Event i Update a Table like as



-(IBAction)bookmarkremoveButtonPressed:(id)sender
{
NSString *docsDir;
NSArray *dirPaths;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
_databasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
@"sampleDatabase.db"]];

const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;

if (sqlite3_open(dbpath, &_myDatabase) == SQLITE_OK)
{
NSString *postId=[self.postId stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Post Id %@",postId);
NSString *querySQL = [NSString stringWithFormat: @"UPDATE ALLREADTABLE Set Bookmark = 0 where PostId ='%@'",postId];
const char *query_stmt = [querySQL UTF8String];

if (sqlite3_prepare_v2(_myDatabase, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(@"error: %s", sqlite3_errmsg(_myDatabase));

if (sqlite3_step(statement) != SQLITE_DONE)
{
NSLog(@"error: %s", sqlite3_errmsg(_myDatabase));
}
else
{
NSLog(@"updateContact SUCCESS - executed command %@",querySQL);
} sqlite3_finalize(statement);
[[NSNotificationCenter defaultCenter]postNotificationName:@"RecordEntryNotification" object:nil];
}
sqlite3_close(_myDatabase);
self.bookMarkButton.hidden=FALSE;
self.bookMarkDoneButton.hidden=TRUE;
}

}


Then my Sql Statement are Printed and Set Value From Bookmark=0 but when i back in to my Bookmark View then Data Was Not Deleted From My Bookmark Table i mean my Data Was Not Updated i try on my ViewWillAppear method write [self.tableView reload data] but it is not working also i tried NSNotification like as in my BookmarkVIew Did load i write as



-(void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadTable) name:@"RecordEntryNotification" object:nil];
}


and reload table method like as



-(void)reloadTable {
[_objects removeAllObjects];
[self getTextFromDb];
[self.tableView reloadData];
}


And i Write on my BookMark Button Remove action



[[NSNotificationCenter defaultCenter]postNotificationName:@"RecordEntryNotification" object:nil];


But it is Not Give me Result as i want please give me Solution for this.


thanks.


Aucun commentaire:

Enregistrer un commentaire