我有一个sqlite数据库,我用它填充表视图。每个单元格相应地打开详细视图。在这个详细视图中,我有一些按钮,它们必须打开其他细节视图,(一个是图像视图,一个是地图视图,一个是下载pdf文件),但所有这些都必须根据表视图行显示内容首先选择。 我怎样才能做到这一点? 感谢名单
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 =@"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
此详细视图上的按钮会打开另一个应显示图像的视图
答案 0 :(得分:0)
所以,基本上你有以下视图控制器:
List ---> Detail ----> Image
|
|--> Map
|
---> PDF
您可以在“List”的prepareForSegue:
方法中将所需的详细信息传递给“详细信息”,并在单击按钮时将它们传递给下一个视图控制器。例如:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showImage"]) {
DetailImageVC *destViewController = segue.destinationViewController;
destViewController.dataImage = self.dataImage;
}
}