1

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
2
  • Reading the code you posted, I'm not clear on the difference between what you have and what you want. Commented Aug 8, 2012 at 21:14
  • when I run this, I get a blank table. Commented Aug 8, 2012 at 21:19

2 Answers 2

2

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.

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

2 Comments

Okay... I am using storyboard not a nib. but i realize i have stuff for nib in my code. This is because I was using other code and forgot to take that out. how would i do this for storyboard.
Thanks for you time. I needed to connect datasource and delegate to my viewcontroller on the view controller tab, and also delete that portion of the nib. Sorry. Thanks though.
1

You need to set the data source

[self setDataSource:self];

And also add the protocol to your interface.

<UITableViewDataSource>

Comments

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.