简体   繁体   中英

cellForRowAtIndexPath is not being called

I had trouble communicating between two views. My objective was to add a new object to my NSMutableArray . Thank to the lovely folks at stackoverflow that was fixed. Now I have another issue.

Even though I'm able to add object to my NSMutableArray , the table is not updating itself to repopulate using the new data. Here is some code:

FirstViewController.h

@interface FirstViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>{  
    IBOutlet UITableView *mytableview;  
    NSMutableArray *mytableinfo;  
@property (nonatomic,retain) IBOutlet UITableView *mytableview;  
@property (nonatomic,retain) NSMutableArray *mytableinfo;`

FirstViewController.m

-(IBAction)addShift:(id)sender{
    SecondViewController *secondViewController = [[SecondViewController alloc]init];
    [self presentModalViewController:secondViewController animated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [mytableinfo count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    // Configure the cell...
    cell.textLabel.text =[mytableinfo objectAtIndex:indexPath.row];
    NSLog(@"Data was written to table");
    return cell;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    mytableinfo = [[NSMutableArray alloc]init];
    mytableview = [[UITableView alloc]init];
    [mytableinfo addObject:@"Hello world1"];
    [self.mytableview reloadData];
}
- (void)viewDidLoad
{
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addShift:)];
    self.navigationItem.rightBarButtonItem = addButton;    
    [super viewDidLoad]; 
    UIBarButtonItem *refresh = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadTableData:)];
    self.navigationItem.leftBarButtonItem = refresh;
    mytableinfo = [[NSMutableArray alloc]init];
    [mytableinfo addObject:@"First Shift"];
    [self.mytableview reloadData];
    // Do any additional setup after loading the view from its nib.
}

SecondViewController.m

#import "SecondViewController.h"
#import "FirstViewController.h"

-(IBAction)saveShift:(id)sender{
    FirstViewController *firstViewController = [[FirstViewController alloc]init];
    firstViewController.mytableinfo = [[NSMutableArray alloc]init];
    [firstViewController.mytableinfo addObject:@"SecondViwController Dismissed"];
    NSLog(@"%@",[firstViewController.mytableinfo objectAtIndex:0]);
    [self dismissModalViewControllerAnimated:YES];
}

So when saveShift is called I should be taken back to main view - which I do- and then repopulate my tableview . It's like I'm getting out of a small problem only to jump into another - not a really good feeling!
Cheers, Sam

in your saveShift:(id)sender method you have to much code. Just do the following :

[self.myTableInfo addObject:@"SecondViewConroller dimissed"];
[self dismissModalViewControllerAnimated:YES];

In that code I'm assuming that you've passed a reference to your NSMutableArray in your secondView.

I remember your post from the NSMutableArray and you are presenting the SecondView from a FirstView (if my memory is good).
But in your code, in the saveShift of the secondView, you are presently creating a brand new FirstView, that is not the same from where you came from. So when you dismiss the modal view your "brand new" firstView get lost and you get back to your original firstView. And that last one haven't hear of your object.


Hi Sam,
go look at the last answer I've given you in your NSMutableArray thread, there is almost everything you need to solve this there.


OK, here is more of the quick fix so that could work

//  SecondViewController.m
#import "SecondViewController.h"
#import "FirstViewController.h"
@implementation SecondViewController
@synthesize dateformatter,mydatepicker,startingTime;
//  HERE Add a new property that match this and a variable in the .h
@synthesize array4Test;
//   HERE make an init that will make this UIViewController know about your NSMutableArray
- (id)initWithMutableArray:aMutableArray
{
self = [self initWithNibName:nil bundle:nil];
if (self)
{
    self.array4Test = aMutableArray;
}
return  self;
}
- (void)dealloc 
{    
//  HERE clean up
self.array4Test = nil;
[super dealloc];
}
-(IBAction)saveShift:(id)sender{
//    HERE remove this code
//FirstViewController *firstViewController = [[FirstViewController alloc]init];
//[firstViewController.mytableinfo addObject:@"Hello world"];
//NSLog(@"%@",[firstViewController.mytableinfo objectAtIndex:0]);
//    HERE add an object to the mutableArray that is store in your firstViewController, the one you've passed the reference in
[self.array4Test addObject:@"This is a String"];
[self dismissModalViewControllerAnimated:YES];
}

I think I've just added an NSLog() in this one.

//  FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation FirstViewController
@synthesize mytableinfo,mytableview;
@synthesize goneToOtherView;
-(IBAction)addShift:(id)sender{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithMutableArray:self.mytableinfo];
[self presentModalViewController:secondViewController animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.mytableview reloadData];
NSString *aString = [mytableinfo lastObject];
if (aString)
{
    NSLog(@"This just came back from the second View\n%@", aString);
}
}

There are at least a couple of problems here.

First problem: you're creating an entirely new FirstViewController in saveShift: , instead of adding an object to the existing FirstViewController . That's not going to work. In addShift: , when you create secondViewController , you should pass the existing FirstViewController as a parameter, like this:

SecondViewController *secondViewController = [[SecondViewController alloc] initWithFirstViewController:self];

You'll need to modify SecondViewController to have an initWithFirstViewController: method which should store the FirstViewController in a property named firstViewController .

Second problem: you're not telling the table view about the new row. You should add a method to FirstViewController :

- (void)reallyAddShift:(NSString *)newShift
{
    [self.mytableinfo addObject:newShift];
    NSIndexPath ip = [NSIndexPath indexPathForRow:[self.mytableinfo count]-1 inSection:0];
    [self.mytableview insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation: UITableViewRowAnimationFade];
}

Then, in saveShift: , you can just do this:

-(IBAction)saveShift:(id)sender{
    [self.firstViewController reallyAddShift:@"SecondViwController Dismissed"];
    [self dismissModalViewControllerAnimated:YES];
}

Assigning simply mytableinfo = [[NSMutableArray alloc]init]; will not retain it. Make that self.mytableinfo = [[NSMutableArray alloc]init]; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM