rivtracks
rivtracks

Reputation: 43

UITableViewController not being filled with NSMutableArray

I have a table view controller, FavoritesTableViewController and it has a mutable array "citiesArray". when a city button is pressed it calls a method which adds an object to the mutable array.

[self.citiesArray addObject:@"New York"];

it then logs the count of self.citiesArray as well as the array itself and appears to be working properly meaning that it successfully added the string "New York" to the array. So now I have a populated mutable array and want to fill the table view with it.

this is my code that doesn't appear to be working.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
       UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
       if (cell==nil) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
       } 
       cell.textLabel.text=[self.citiesArray objectAtIndex:indexPath.row];
       return cell;
}

I've tried reloading my table view after but still doesn't work. Any ideas as to what the problem is? thanks

Upvotes: 0

Views: 42

Answers (2)

xexe
xexe

Reputation: 346

Imlement the methods:

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

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

And, of course, if you haven't set the datasource and delegate property of your table, you should do it, for example in ViewDidLoad method:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //....Your code....

    table.delegate = self;
    table.datasource = self;
}

Upvotes: 1

Greg
Greg

Reputation: 25459

You have to add method to return how many rows are in the table:

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

Upvotes: 1

Related Questions