0

I have 2 VCs, first is BooksDetailViewController which has the NSString myBookString. Another on is FavBooksViewController which has the favBookList NSMutableArray. I want to pass the NSString myBookString from the 1st VC to include it in the favBookList NSMutableArray which then populates the table.

I have included @class FavBooksViewController; in the BooksDetailViewController.h

Imported #import "FavBooksViewController.h" in the BooksDetailViewController.m

The action in the BooksDetailViewController.m looks like this:

- (IBAction)addToFav:(id)sender {
    FavBooksViewController *mdvc;
    [mdvc.self.favBookList addObject:myBookString];
}

In FavBooksViewController.h I have

@interface FavBooksViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) id delegate;
@property (nonatomic,strong) NSMutableArray *favBookList;

In the FavBooksViewController.m - #import "FavBooksDetailViewController.h" In the ViewDidLoad I tried to include this line

self.favBookList = [[NSMutableArray alloc]initWithObjects:nil];

But I have commented it out. With it our without it the UITableView isn't working. And of course I have this:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.favBookList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
           UITableViewCell *cell = [tableView 
                                 dequeueReusableCellWithIdentifier:@"favBooksCell"];

        cell.textLabel.text = [self.favBookList
                               objectAtIndex:indexPath.row];
        return cell;
}

No matter what, the UITableView is just empty, no cells, nothing. What can be the possible mistake here? What am I doing wrong? Thanks in advance!

3
  • Are you sure the object is added to the NSMutableArray? Commented Jul 27, 2012 at 8:31
  • Hey SergiusGee, just to be sure. have you linked your tableView with the delegate/datasource? It happens to me sometimes :-) Commented Jul 27, 2012 at 8:33
  • @iGranDav Yeah, I sure have :) Commented Jul 27, 2012 at 8:59

2 Answers 2

2

Instead of adding the string to the array in the BooksDetailViewController , just pass the string to the FavBooksViewController and then add it to the array in your FavBooksViewController.

- (IBAction)addToFav:(id)sender {
    FavBooksViewController *mdvc;
    [mdvc setFavBook:myBookString];  
    //favBook is a NSString member object in your FavBooksViewController class

     [self.navigationController pushViewController:mdvc animated:YES];
}

Just try if this works.

Sign up to request clarification or add additional context in comments.

Comments

2

You forgot to initiates the cell in your cellForRowAtIndexPath method.

The code should be :

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

        UITableViewCell *cell = [tableView 
                                 dequeueReusableCellWithIdentifier:identifier];
        if(!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }

        cell.textLabel.text = [self.favBookList
                               objectAtIndex:indexPath.row];
        return cell;
}

Try this ;-)

Hope this helps.

2 Comments

You have to do this, but it seems that your array is still empty. Try to remove self in your addToFav method. You now should have [mdvc.favBookList addObject:myBookString];
It's the first thing I asked. Are you sure the object is added to the NSMutableArray?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.