Craig
Craig

Reputation: 1085

How to load array into a UITableView, not TableViewController

I am trying to take an array that I loaded from another viewer and put it into a UITableView, not a TableViewController. I don't know how I would do this. Right now I have this code:

CRHCommentSection.m

#import "CRHCommentSection.h"
#import "CRHViewControllerScript.h"

@interface CRHCommentSection ()

@end

@implementation CRHCommentSection
@synthesize observationTable;

NSArray *myArray; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}


- (void)viewDidLoad
    {


    myArray = [CRHViewControllerScript theArray];
    NSLog(@"%@", myArray);
    //NSArray* paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0   inSection:1]];
    //[observationTable insertRowsAtIndexPaths:paths   withRowAnimation:UITableViewRowAnimationTop];


    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.

    NSLog(@" in method 1");
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog(@" in method 2");
   // Return the number of rows in the section.
   return [myArray count];


   }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath {

    NSLog(@" in method 3");

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:CellIdentifier];
    }

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

CRHCommentSection.h

    #import <UIKit/UIKit.h>


@interface CRHCommentSection : UIViewController

@property (weak, nonatomic) IBOutlet UITableView *observationTable;



@end

Upvotes: 1

Views: 1622

Answers (2)

deleted_user
deleted_user

Reputation: 3805

Since you have posted this same example code more than once, Ill give you the way to do this without using statics. And Yes you do need to set the collection (array) on the view controller not the UITableView as the tableView uses the controller as a datasource.

Lets say you had a UITableView that displayed search results... And you launch this view controller from either some other view or from the application delegate like so..

In your viewController (yours is CRHCommentSection) define a property for the array you are going to populate the table with.

@property (nonatomic,retain, setter=setSearchResults:) NSArray *searchResults; //use your array name here
//BTW please dont call it ARRAY..its confusing.

Also in your commentsController (which would be a better name that what you have) add the setter for the array

-(void) setSearchResults:(NSArray *)searchResults
{
    //in case we loaded this view with results previously release the previous results
    if ( _searchResults )
    {
        [_searchResults release];
    }
    _searchResults = [searchResults retain]; 

   [_tableView reloadData];
}

When you instantiate the new view controller with the UITableView in it - set the "array", in your case comments in my case searchResults

_searchResultsViewController = [[SearchResultsViewController alloc] initWithNibName:@"SearchResultsViewController" bundle:nil];

//now set the array on the controller
_searchResultsViewController.searchResults = mySearchResults; //(your array)

Thats a simple way to communicate the array for the table view when you instantiate a view controller.

Also the naming conventions will help you clear things up

If you have a view controller for comments it should be

CommentsViewController

And the array if it contains comments should be called comments - for readability.

Cheers.

As for the rest of the boilerplate setting the datasource and delegate and cellForRow etc, you got that advice on the last go around so I wont repeat it here.

@bigkm had a good point

@interface SearchResultsViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

Your view controller has to be defined properly as above with the protocols.

Upvotes: 2

bigkm
bigkm

Reputation: 2277

You need to set the data source

[self setDataSource:self];

And also add the protocol to your interface.

<UITableViewDataSource>

Upvotes: 1

Related Questions