#import <sqlite3.h>
#define DbName @"BibleDB.sqlite"
@interface DbHandler : NSObject {
}
+(void)createEditableCopyOfDatabaseIfNeeded;
+(NSString *) dataFilePath:(NSString *)path;
// Malayalam Version
+(int)mNumberOfChaptersInBook:(NSString *)book;
+(int)mNumberOfVerseForChapter:(NSString *)chapter andBook:(NSString *)book;
+(NSMutableArray *)mVerseForChapter:(NSString *)chapterNo OfBook:(NSString *)book;
+(NSString *)mVerseForVerseNo:(NSString *)verseNo OfChapter:(NSString *)chapterNo OfBook:(NSString *)book;
//+(NSString *)mVerseForVerseNo:(NSString *)verseNo OfChapter:(NSString *)chapterNo OfBook:(NSString *)book FromDB:(sqlite3 *)database;
// English Version
//+(int)eNumberOfChaptersInBook:(NSString *)book;
+(NSMutableArray *)eVerseForChapter:(NSString *)chapterNo OfBook:(NSString *)book;
+(NSMutableArray *)verseForSearchTag:(NSString *)searchTag;
//Hindi version
+(NSMutableArray *)hVerseForChapter:(NSString *)chapterNo OfBook:(NSString *)book;
+(int)hNumberOfChaptersInBook:(NSString *)book;
//+(NSMutableArray *)hverseForSearchTag:(NSString *)searchTag;
//Bookmarks
+(void)createBookmark:(NSString *)text :(NSString *)book :(NSString *)chapter :(NSString *)verse;
+(NSMutableArray *)getBookmarks;
+(bool)deleteBookmark:(NSString *)book :(NSString *)chapter :(NSString *)verse;
@end
的.m
#pragma mark Englisg DB
+(NSMutableArray *)eVerseForChapter:(NSString *)chapterNo OfBook:(NSString *)book
{
NSMutableArray * result = [[NSMutableArray alloc] init];
sqlite3 *database;
NSString *dbpath;
dbpath = [DbHandler dataFilePath:DbName];
if (sqlite3_open([dbpath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSql = [NSString stringWithFormat:@"SELECT [text] FROM english where book = '%@' and chapterNo = '%@'",book,chapterNo];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
[result addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]] ;
}
sqlite3_finalize(statement);
}
else
{
NSLog(@"Sql Preparing Error");
}
sqlite3_close(database);
}
else
{
NSLog(@"Database not opening");
}
return result;
}
+(NSMutableArray *)verseForSearchTag:(NSString *)searchTag
{
NSMutableArray * result = [[NSMutableArray alloc] init];
sqlite3 *database;
NSString *dbpath;
dbpath = [DbHandler dataFilePath:DbName];
if (sqlite3_open([dbpath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSql = [NSString stringWithFormat:@"SELECT [text],book,chapterNo,verseNumber FROM english where [text] like '%%%@%%%' limit 0,500",searchTag];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
[result addObject:[[NSMutableDictionary alloc] init]] ;
[[result lastObject] setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)] forKey:@"text"];
[[result lastObject] setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)] forKey:@"book"];
[[result lastObject] setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)] forKey:@"chapter"];
[[result lastObject] setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)] forKey:@"verse"];
}
sqlite3_finalize(statement);
}
else
{
NSLog(@"Sql Preparing Error");
}
sqlite3_close(database);
}
else
{
NSLog(@"Database not opening");
}
return result;
}
+(int)hNumberOfChaptersInBook:(NSString *)book
{
sqlite3 *database;
NSString *dbpath;
dbpath = [DbHandler dataFilePath:DbName];
if (sqlite3_open([dbpath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSql = [NSString stringWithFormat:@"SELECT count(*) FROM hindi where book = '%@'",book];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
return [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)] intValue];
}
}
else
{
NSLog(@"Sql Preparing Error");
}
}
else
{
NSLog(@"Database not opening");
}
return 0;
}
#pragma mark DB Setup Functions
+(void)createEditableCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DbName];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) {
NSLog(@"Database file already exist, so returning...");
return;
}
NSLog(@"CREATING A NEW COPY OF THE DATABASE...");
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DbName];
//[fileManager removeItemAtPath:writableDBPath error:nil];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
//Some serious problem...
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
+(NSString *) dataFilePath:(NSString *)path
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:path];
}
@end
这是我用于显示此db
的tableview代码static NSString *CellIdentifier = @"Cell";
readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"readCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.malayalamVerse.hidden = YES;
cell.malayalamVerse.backgroundColor = [UIColor clearColor];
// self.table.tableFooterView = refreshFooterView;
//self.table.tableFooterView.userInteractionEnabled = YES;
}
if(tableView == table)
{
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
// myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
[myBackView setBackgroundColor:[UIColor clearColor]];
cell.selectedBackgroundView = myBackView;
[myBackView release];
cell.textLabel.highlightedTextColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
table.backgroundColor = [UIColor clearColor];
table.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:@"%@.%d ",delegate.selectedChapter, indexPath.row+1];
cell.chapterAndVerse.backgroundColor= [UIColor whiteColor];
cell.chapterAndVerse.textColor = [UIColor brownColor];
cell.chapterAndVerse.font = [UIFont fontWithName:@"Georgia" size:14.0];
cell.chapterAndVerse.frame=CGRectMake(33, 6, 30.0, 12.0);
cell.textLabel.text = [NSString stringWithFormat:@" %@",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];
cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:18];
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.backgroundColor = [UIColor clearColor];
}
请帮我这样做,如果有人对此有所了解请与我分享。 非常感谢。
答案 0 :(得分:2)
这个问题已经存在了一段时间,所以你可能已经解决了这个问题。
你已经发布了很多代码,但是如果我理解这个问题,那么需要发生的主要问题是在tableView:cellForRowAtIndexPath:方法中,应用程序根据选择的语言做了不同的事情。
两种方法:
数据库对象中的(1)(在代码中由delegate
指向)对于所有语言都有一种方法,例如`allSelectedVerse:'。然后,在数据库对象方法中,对语言进行测试并返回相应的文本。
或(2)在tableView:cellForRowAtIndexPath:方法中运行所选语言的测试,并相应地调用数据库对象上的不同方法。
无论哪种方式,您都需要一些存储所选语言的方法。这可以是数据库对象,也可以是用于跟踪设置或以多种不同方式跟踪的单例。
虽然(2)不太优雅,但需要的代码较少。假设使用NSString属性和将当前选择的语言存储在数据库对象中,假设每当用户更改语言时,您发送tableView reloadData
方法,以下代码应该执行此操作。 (所有这些假设我首先理解你的问题......)
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *CellIdentifier = @"Cell";
readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[self configureCellOnFirstCreation: cell]; // farm out configuration
}
if (tableView == table)
{
[self configureCell: cell]; // farm out cell configuration, so the content setting code is more prominent
cell.chapterAndVerse.text = [NSString stringWithFormat:@"%@.%d ",delegate.selectedChapter, indexPath.row+1];
if ([[delegate selectedLanguage] isEqualToString: @"Malayalam"])
{
cell.textLabel.text = [NSString stringWithFormat:@" %@",[delegate.allSelectedVerseMalayalam objectAtIndex:indexPath.row]];
}
else if ([[delegate selectedLanguage] isEqualToString: @"Hindi"])
{
cell.textLabel.text = [NSString stringWithFormat:@" %@",[delegate.allSelectedVerseHindi objectAtIndex:indexPath.row]];
}
else // default to English is no matching string found
{
cell.textLabel.text = [NSString stringWithFormat:@" %@",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];
}
}
else
{
// handle the other case, e.g. when we're doing search
}
}
尾注如果您可以在问题中发布更少的代码,我想您可能会获得更多更快的答案。在上面,我已经将配置转换为其他方法,因此重要的代码流程更加突出。