views:

1657

answers:

3

I need help, I need to populate a tableview with objects from my array... I may be using some methods from mac code (lol). I just need some help. Here is my header and implementation files, thanks.

#import <UIKit/UIKit.h>

@interface Chuck_FactsViewController : UIViewController {

IBOutlet UITableView *tableView;
NSMutableArray *chuckJokes;
}

- (IBAction)newView:(id)sender;

@end


@implementation Chuck_FactsViewController

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}

- (void)dealloc {
[super dealloc];
chuckJokes = [[NSMutableArray alloc] init];

 }

 - (id)init {
[super init];
return self;
 }

 -(void)awakeFromNib {
chuckJokes = @"joke 1", @"joke 2", nil;
 }

 - (IBAction)newView:(id)sender {
 }

 - (NSInteger)numberOfSections:(NSInteger)row {
return [chuckJokes count];
 }

 - (id)tableView:(UITableView *)tv row:(int)row {
NSString *v = [chuckJokes objectAtIndex:row];
return v;
 }

 @end

Sorry, I'm an idiot. Some of this may not make sense

Thanks

+3  A: 

This line doesn't make any sense at all; perhaps something was lost in the formatting?

chuckJokes = @"joke 1", @"joke 2", nil;

It should be:

chuckJokes = [[NSMutableArray alloc] initWithObjects:@"joke 1", @"joke 2", nil];

(and it would be better in your init method).

Also, you should be releasing chuckJokes in your dealloc, not creating it--all that does is leak an empty array on deallocation--and [super dealloc] should always be the last call in your dealloc. If you aren't familiar with Cocoa programming you might want to read some introductory documentation about the life-cycle of objects.

Beyond that it looks like you should read the iPhone table view guide and then ask specific questions if you have problems after that.

smorgan
Thanks for the answer. Sorry the question was kind of stupid.
Daniel Kindler
+2  A: 

You also miss some DataSource methods:

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


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"chuck"];
    if (cell == nil) {
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"chuck"] autorelease];
    }
    cell.text = [chuckJokes objectAtIndex:[indexPath.row]];

    return cell;
}

And finaly I don't know what's doing method

- (NSInteger)numberOfSections:(NSInteger)row

My assumption is that you're trying to call

- (NSInteger)numberOfSections

but, if you have only one section in table view default value is 1 and you can skip this method.

My advice is: buy and read some good iPhone development books.

mperovic
+1  A: 
- (void)dealloc {
[super dealloc];
chuckJokes = [[NSMutableArray alloc] init];

 }

geee, what is that doing in the 'dealloc' method?