в интерфейсе:
NSMutableArray *booktable;
NSMutableArray *sbook;
NSMutableArray *rowoflibrary;
NSString *databaseName;
NSString *databasePath;
IBOutlet UISegmentedControl *segControl;
IBOutlet UITableView *libraryTV;
IBOutlet UISearchBar *searchBar;
в реализации
- (void)viewDidLoad {
databaseName = @"MydataBase.db";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
// Execute the "checkAndCreateDatabase" function
[self checkAndCreateDatabase];
[self readbookFromDatabase];
booktable = [[NSMutableArray alloc]init];
[booktable addObjectsFromArray:sbook];
rowoflibrary = [[NSMutableArray alloc] init];
[super viewDidLoad];}
-(void) checkAndCreateDatabase{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:databasePath];
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
[fileManager release];}
-(void) readbookFromDatabase {
// Setup the database object
sqlite3 *database;
// Init the staff Array
sbook = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement ="select book.title,author.auth_name ,publisher.pub_name , book.info from book ,author, publisher where book.auth_id = author.auth_id and book.pub_id = publisher.pub_id";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
char * str = (char*)sqlite3_column_text(compiledStatement, 0);
if (str){
title = [NSString stringWithUTF8String:str];
}
else{
title =@" ";
}
char * str1 = (char*)sqlite3_column_text(compiledStatement, 1);
if (str1){
authorName = [NSString stringWithUTF8String:str1];
}
else{
authorName =@" ";
}
char * str2 = (char*)sqlite3_column_text(compiledStatement, 2);
if (str2){
pubName = [NSString stringWithUTF8String:str2];
}
else{
pubName =@" ";
}
char * str3 = (char*)sqlite3_column_text(compiledStatement,3);
if (str3){
info = [NSString stringWithUTF8String:str3];
}
else{
info =@" ";
}
// Create a new book object with the data from the database
book *bok = [[book alloc] initWithbook:title autname:authorName pubname:pubName infobook:info];
// Add the book object to the staff Array
[sbook addObject:bok];
[bok release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);}
- (void) сегментSelected: (UISegmentedControl *) отправитель {
switch (sender.selectedSegmentIndex) {
случай 0:
[self loadTitles];
перерыв;
Случай 1:
[self loadAuthors];
перерыв;
случай 2:
[self loadPublishers];
перерыв;
дефолт:
перерыв; }}
- (void) loadTitles
{
[rowoflibrary removeAllObjects];
for (book *bok in self.booktable)
{
[rowoflibrary addObject:bok.book_title];
}
[libraryTV reloadData];
}
}
}
Прагма Марк -
pragma mark Источник данных табличного представления
} * * тысяча пятьдесят-один
(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {
статический NSString * CellIdentifier = @ "Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero reuseIdentifier: CellIdentifier] autorelease];
}
// Настройка ячейки
cell.textLabel.text = [rowoflibrary objectAtIndex: indexPath.row];
return cell;
}
при запуске это пустое представление таблицы