开发者

dimissModalViewControllerAnimated causing tableView to be full screen?

I have a RootViewController with 2 tableViews as subviews (created in IB) each with their own tableViewController class (handleing fetchRequests etc.)

1 tableView is static (no data changed by user or modelViews).

tableView 2 has a button in the header which presents an imagePickerController. No issues so far.

Problem is, when i dismiss the imagePicker

 [self 开发者_运维问答dismissModalViewControllerAnimated:YES];

TableView 2 becomes full screen i have tried

[[self rootViewController] dismissModalViewControllerAnimated:YES]

Nothing happens at all. It sticks on the image picker.

I suspect this is due to there being very little of the view being created programmaticaly.

Any ideas?

Thanks in advance.

DetartrateD

 -(IBAction)addImageTableAPressed {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];  
}

            RootViewController
             ||            ||
             ||            ||    
             \/            \/          addImageTableAPressed
  TableViewControlA  TableViewControlB --------------------->modelViewController

To resolve mananagedObjectContect.....

     - (void)viewDidLoad {...
 if(managedObjectContext == nil) 
{ 
    managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    NSLog(@"After managedObjectContext: %@",  managedObjectContext);
}
   ...
  }


As I mentioned in one of my comments, I would prefer having a single view controller managing the two table views. Define a UIView (the rootView) including 2 subviews (tableViewA and tableViewB). Your RootViewController's view will be rootView, and this controller will have to be the data source and delegate of both table views. The code I will give here is by no means complete nor optimal, but gives you a good idea of what is needed to implement my solution.

For example:

@interface RootViewController <UITableViewDelegate, UITableViewDataSource> {
    NSArray *dataArrayA;
    NSArray *dataArrayB;
    UITableView tableViewA;
    UITableView tableViewB;
    NSManagedObjectContext *context;
}

@property (nonatomic, retain) NSArray *dataArrayA;
@property (nonatomic, retain) NSArray *dataArrayB;

// in IB, link the dataSource and delegate outlets of both tables to RootViewController
@property (nonatomic, retain) IBOutlet UITableView tableViewA;
@property (nonatomic, retain) IBOutlet UITableView tableViewB;

// this property will allow you to pass the MOC to the RootViewController from 
// the parent view controller, instead of accessing the app delegate from RootViewController
@property (nonatomic, retain) NSManagedObjectContext *context;

// ... etc.

@end



@implementation RootViewController

@synthesize dataArrayA;
@synthesize dataArrayB;
@synthesize tableViewA;
@synthesize tableViewB;
@synthesize context;

// initialize dataArrayA and dataArrayB
- (void)viewDidLoad {
    [super viewDidLoad];

    NSError *error = nil;

    // initialize and configure your fetch request for data going into tableViewA
    NSFetchRequest fetchRequestA = [[NSFetchRequest alloc] init];

    // configure the entity, sort descriptors, predicate, etc.
    // ...

    // perform the fetch
    self.dataArrayA = [context executeFetchRequest:fetchRequestA error:&error];

    // do the same for the data going into tableViewB - the code is very similar, you
    // could factor it out in a private method instead of duplicating it here
    // NSFetchRequest fetchRequestB = [[NSFetchRequest alloc] init]; 

    // omitting the details ... etc.

    self.dataArrayB = [context executeFetchRequest:fetchRequestB error:&error];

    // release objects you don't need anymore, according to memory management rules
    [fetchRequestA release];
    [fetchRequestB release];
}


// Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // if you have a different number of sections in tableViewA and tableViewB
/*
    if (tableView == tableViewA) {
        return ??;
    } else {
        return ??
    }
*/

    // otherwise, if both table views contain one section
    return 1;
}

// Customize the number of rows in each table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == tableViewA) {
        return [dataArrayA count];
    } else {
        return [dataArrayB count];
    }
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;

    if (tableView == tableViewA) {
        // get the data for the current row in tableViewA
        id objectA = [dataArrayA objectAtIndex:indexPath.row];

        // configure the cell for tableViewA
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierA];

        // etc...
    } else {
        // get the data for the current row in tableViewB
        id objectB = [dataArrayB objectAtIndex:indexPath.row];

        // configure the cell for tableViewB
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierB];
        // etc...
    }
    return cell;
}

// And so on, the same idea applies for the other UITableViewDelegate you would need to 
// implement...


- (void)dealloc {
    [dataArrayA release];
    [dataArrayB release];
    [tableViewA release];
    [tableViewB release];
    [context release];

    // etc...

    [super dealloc];
}

@end

I hope you'll find this useful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜