0

I have UIViewController that contains NSMutableArray , I want to pass this array to UITableViewController and view it on the table .. how can I do that ??

I mean I want to (pass) NSMutableArray or any Variable from UIViewController to UITableViewController not (create) a table

I want to pass newBooksArray to UITableViewController, I wrote in UIViewController:

mytable.gettedBooks = newBooksArray; // gettedBooks is NSMutableArray in UITableViewController

mytable.try = @"Emyyyyy"; // try is a NSString in UITableViewController

and in UITableViewController in DidloadView i wrote

NSLog(@"Try: %@", try); // out is null
NSLog(@"my getted array count: %d", [gettedBooks count]); // out is 0

any help ???

6
  • can you give me an example ?? Commented Apr 21, 2012 at 13:12
  • what you are tried. show ur code..... Commented Apr 21, 2012 at 13:12
  • You need to start here: Table View Programming Guide for iOS Commented Apr 21, 2012 at 13:16
  • from here mobile.tutsplus.com/tutorials/iphone/uitableview Commented Apr 21, 2012 at 13:17
  • 1
    @Jenox try is a reserved keyword in Objective-C++ not Objective-C, however, it is still not a good idea for anyone to use as a variable name or type identifier. Commented Apr 23, 2012 at 22:48

3 Answers 3

2

Creating a UITableView and filling with an array

I created the above tutorial specially for this problem.

There are also more methods you can learn about on the developer documents

Firstly, you want to make sure you have the required delegate calls in your @interface:

@interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray         * feed;
    UITableView            * tableView;
}

You want something similar to the following in your controller:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [feed 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];
    }

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

numberOfRowsInSection makes sure you actually load the required number of cell rows from your NSMutableArray. And cellForRowAtIndexPath actually loads the content from each row of your NSMutableArray into each row of the UITableView.

For passing it to another controller, don't you want something like this?

UITableViewController *viewController = [[UITableViewController alloc] initWithNibName:@"TableXIB" bundle:nil];
[viewController setGettedBooks:newBooksArray];
[self.navigationController pushViewController:viewController animated:YES];
Sign up to request clarification or add additional context in comments.

6 Comments

I didn't watch the video, but you're missing a bunch of delegate methods in your example code here. They might help others who come to the question.
Any better? - I added the numberOfRowsInSection as well.
Yup. Sorry, I should have said one data source method. I wasn't thinking. Looks good now!
I'm using xcode 4.3 , I just want to pass array from UIViewController to UITableView Controller - not creating the table
mean I want to open the table and see the array got from UITableViewController and print it using NSLog
|
1

UITableview tutorial and sample code

Hope,this will help you..enjoy

Comments

0

If you are using a Tab Bar controller, and First View controller is table view controller and second is UIView controller. You can Pass data to Table View controller by followoing code segment. You need to declare variable called arrayData (NSMutableArray) in table view controller and set property (Since we need to access this from another class.) From this arrayData, you need to load data in tableView. In View controller class write following code.

    NSArray *viewControllers = [self.tabBarController viewControllers];
    MyTableViewController *mTable = [viewControllers objectAtIndex:0];

    [mTable SetArrayData:arrayFromViewController];

    [mTable.tableView reloadData];

If you are using Navigation controller, you can do

    NSArray *viewControllers = [self.navigationController viewControllers];
    MyTableViewController *mTable = [viewControllers objectAtIndex:0];

    [mTable SetArrayData:arrayFromViewController];

    [mTable.tableView reloadData];

Optionally you can use delegates.

1 Comment

sorry but I cant understand where the array content .. can U Illuminate more please ??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.