0

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

3
  • Dont forget to check the table view delegate. Commented Mar 29, 2014 at 17:15
  • @pawan, the delegate has nothing to do with this problem. The data source could be the problem if the OP hasn't set it, but the delegate methods aren't relevant to getting the data to appear. Commented Mar 29, 2014 at 17:20
  • @rdelmar my fault, mistakenly i have written delegate, it should be datasource. Commented Mar 29, 2014 at 17:39

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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;
}

2 Comments

its the same posted by @Greg.
numberOfSectionsInTableView: is optional if you only have one section.

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.