我m working on a splitview app , where the master view is a table view fetched from sqlite database ,when the user tab a row the detail view UITableView get updated by data also fetched from the same sqlite database.
I
能够填充主视图表视图,但是不能获取详细视图表视图以使用新数据进行更新吗?
我到处搜索如何做到这一点,但没有用,大多数教程和答案都是关于标签或webview,但从未发现有关detailview表视图。
我正在使用stroyboard和xcode 4.5.2
这里是masterviewcontroller.m didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Destinations *dest = [self.destinations objectAtIndex:[indexPath row]];
self.detailViewController.title = dest.destName;
self.detailViewController.detailItem = dest.destID;
[self.tableView reloadData];
}
这是detailviewcontroller.h
#import <UIKit/UIKit.h>
#import "FMDatabase.h"
@interface DetailViewController : UIViewController<UITableViewDelegate, UITableViewDataSource,UISplitViewControllerDelegate>
{
NSMutableArray *alternates;
UITableView *alternateTableView;
}
@property (nonatomic,strong) NSMutableArray *alternates;
@property (strong, nonatomic) id detailItem;
@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@property (strong, nonatomic) IBOutlet UITableView *alternateTableView;
@property (retain, nonatomic) NSString *pdestid;
@end
detailviewcontroller.m
#import "DetailViewController.h"
#import "Alternates.h"
@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end
@implementation DetailViewController
@synthesize pdestid, alternates,alternateTableView;
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
self.pdestid = [self.detailItem description];
NSFileManager * fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex: 0];
NSString * writableDBPath = [documentsDirectory stringByAppendingPathComponent: @"alternates.s3db"];
BOOL success = [fileManager fileExistsAtPath: writableDBPath];
if (! success) {
NSString * defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"alternates.s3db"];
success = [fileManager copyItemAtPath: defaultDBPath toPath: writableDBPath error: & error];
}
FMDatabase * db = [FMDatabase databaseWithPath: writableDBPath];
if (! [db open])
{
NSLog (@"Err% d:%@", [db lastErrorCode], [db lastErrorMessage]);
}
[db setShouldCacheStatements: YES];
NSString * sql = [NSString stringWithFormat:@"SELECT * FROM Alternates WHERE destID = '%@' ORDER BY altName ASC;", pdestid];
FMResultSet * rs = [db executeQuery: sql];
alternates = [[NSMutableArray alloc] init];
while ([rs next])
{
Alternates * alter = [[Alternates alloc] init];
alter.altname = [rs stringForColumn: @"altName"];
alter.alticao = [rs stringForColumn: @"altICAO"];
alter.dist = [rs stringForColumn:@"distance"];
alter.mgtrk =[rs stringForColumn:@"magtrack"];
alter.route = [rs stringForColumn:@"route"];
[alternates addObject:alter];
}
[rs close];
[db close];
}
}
- (void)viewDidLoad
{
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
[self configureView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return alternates.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AlternateCell"];
Alternates *alter = [self.alternates objectAtIndex:[indexPath row]];
cell.textLabel.text = alter.altname;
return cell;
}
#pragma mark - Split view
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
barButtonItem.title = NSLocalizedString(@"DESTINATIONS", @"DESTINATIONS");
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = popoverController;
}
- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
// Called when the view is shown again in the split view, invalidating the button and popover controller.
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
self.masterPopoverController = nil;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
- (void)viewDidUnload {
[self setAlternateTableView:nil];
[super viewDidUnload];
}
@end
答案 0 :(得分:0)
在您的DetailViewController
中,您是否在从configureView
获取数据库中的数据后重新加载表格视图?
尝试在表格视图中添加reloadData
:
- (void) configureView
{
// [... as shown above ...]
[self.alternateTableView reloadData];
}
答案 1 :(得分:0)
如果你的详细信息是tableviewController,你可能想在configureView中添加[self.tableView reloadData]