0
我有一個sqlite數據庫,我用它填充表視圖。每個單元都會相應地打開詳細視圖。在這個細節視圖中,我有一些按鈕需要打開其他細節視圖(一個是圖像視圖,一個是地圖視圖,一個是下載PDF文件的視圖),但所有這些都必須根據表視圖行顯示內容首先選擇。 我該怎麼做? 感謝名單詳細視圖按鈕打開另一個詳細視圖根據sqlite db
TableVC.m
#import "TableVC.h"
#import "DetailPointsVC.h"
@interface TableVC()
@end
@implementation TableVC {
NSMutableArray *listOfTitle;
NSMutableArray *listOfImages;
}
@synthesize sendDataTable;
- (void)viewDidLoad
{
[super viewDidLoad];
[self initDatabase];
[self getPoints];
}
#pragma mark - View lifecycle
-(void)initDatabase{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"mydb.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
{
return;
}
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mydb.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success)
{
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
-(void)getPoints{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"geositidb.sqlite"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
const char *sql = "SELECT * FROM pointsofinterest";
sqlite3_stmt *searchStatement;
if (sqlite3_prepare_v2(database, sql, -1, &searchStatement, NULL) == SQLITE_OK)
{
listOfTitle = [[NSMutableArray alloc] init];
listOfImages = [[NSMutableArray alloc] init];
while (sqlite3_step(searchStatement) == SQLITE_ROW)
{
NSString *title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 2)];
NSString *image = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 10)];
[listOfTitle addObject:title];
[listOfImages addObject:image];
}
}
sqlite3_finalize(searchStatement);
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [listOfTitle count];
}
//Table View cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier [email protected]"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(cell == nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [listOfTitle objectAtIndex:indexPath.row];
return cell;
}
//Detail View items
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.sendDataTable indexPathForSelectedRow];
DetailPointsVC *destViewController = segue.destinationViewController;
destViewController.dataName = [listOfTitle objectAtIndex:indexPath.row];
destViewController.dataImage = [listOfImages objectAtIndex:indexPath.row];
}
}
@end
DetailPointsVC.h
@interface DetailPointsVC : UIViewController
//UILabel
@property (nonatomic, strong) IBOutlet UILabel *dataLabel;
@property (nonatomic, strong) NSString *dataName;
//UIImageView
@property (weak, nonatomic) IBOutlet UIImageView *dataImageView;
@property (nonatomic, strong) NSString *dataImage;
DetailPointsVC.m
@interface DetailPointsVC()
@end
@implementation DetailPointsVC
@synthesize dataLabel;
@synthesize dataName;
@synthesize dataImageView;
@synthesize dataImage;
- (void)viewDidLoad {
[super viewDidLoad];
self.dataLabel.text = dataName;
self.dataImageView.image = [UIImage imageNamed:dataImage];
}
@end
在此詳細視圖的按鈕以打開該圖像應顯示
我有這樣的桌子上view.m文件: - (無效)prepareForSegue:(UIStoryboardSegue *)賽格瑞發件人:(ID)發送{ 如果([segue.identifier isEqualToString:@「showDetail 「]){ NSIndexPath * indexPath = [self.sendDataTable indexPathForSelectedRow]; DetailPuntiVC * destViewController = segue.destinationViewController; destViewController.dataName = [listOfTitolo objectAtIndex:indexPath.row]; 根據單元打開詳細視圖。在這個細節視圖中,我有一個按鈕來打開另一個細節視圖,它將根據我剛打開的細節視圖顯示一個pdf。 – Max1980
對不起,我誤解了你的問題。那麼你已經做了正確的事情 - 也許你需要從列表中傳遞更多細節到細節?如果我當前的回答不夠好,請發佈相關視圖控制器的更多詳細信息。沒有看到他們的代碼很難幫助他人。 – Glorfindel
框架是正確的。我有:Table.m 實現TableVC { NSMutableArray * listOfImages;} listOfImages = [[NSMutableArray alloc] init;NSString * immagine = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement,10)]; PrepareForSegue中的 :destViewController.dataImage = [listOfImages objectAtIndex:indexPath.row]; 在詳細視圖中:property(weak,nonatomic)IBOutlet UIImageView * dataImageView; 屬性(非原子,強)NSString * dataImage; 但圖像不應該在這裏打開因爲沒有圖像視圖,而是在另一個視圖(在詳細視圖中按鈕打開) – Max1980