moomoo
moomoo

Reputation: 77

Table view isnt working correctly.

I am trying to create a table view pragmatically(no xib and no storyboards), but the food array isn`t showing up in table view at all. I will post all my code in view controller.

Viewcontroller.h

  @interface searchViewController :     ViewController<UITextFieldDelegate,UISearchBarDelegate,UITableViewDataSource>{
  NSMutableArray * getterms;
}

 @property (strong, nonatomic) NSMutableArray* allTableData;
 @property (strong, nonatomic) NSMutableArray* filteredTableData;
 @property (nonatomic, assign) bool isFiltered;
 @property (nonatomic, retain) UITableView *tableView;

Viewcontroller.m

  -(void)viewDidLoad{
   UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(10.0, 10.0, 1000.0, 200.0) style:UITableViewStylePlain];
[self.view addSubview:tableView];
  self.tableView.dataSource = self;

     allTableData = [[NSMutableArray alloc] initWithObjects:
                [[Food alloc] initWithName:@"Steak" andDescription:@"Rare"],
                [[Food alloc] initWithName:@"Steak" andDescription:@"Medium"],
                [[Food alloc] initWithName:@"Salad" andDescription:@"Caesar"],
                [[Food alloc] initWithName:@"Salad" andDescription:@"Bean"],
                [[Food alloc] initWithName:@"Fruit" andDescription:@"Apple"],
                [[Food alloc] initWithName:@"Potato" andDescription:@"Baked"],
                [[Food alloc] initWithName:@"Potato" andDescription:@"Mashed"],
                [[Food alloc] initWithName:@"Bread" andDescription:@"White"],
                [[Food alloc] initWithName:@"Bread" andDescription:@"Brown"],
                [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Beef"],
                [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Chicken"],
                [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Veggie"],
                [[Food alloc] initWithName:@"Pizza" andDescription:@"Pepperonni"],
                nil ];
           }

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

static NSString *MYCellIdentifier = @"MyCellIdentifier";

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MYCellIdentifier];
if (cell == nil)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MYCellIdentifier];

Food* food;
if(isFiltered)
    food = [filteredTableData objectAtIndex:indexPath.row];
else
    food = [allTableData objectAtIndex:indexPath.row];

cell.textLabel.text = food.name;
cell.detailTextLabel.text = food.description;

return cell;

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


     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int rowCount;
if(self.isFiltered)
    rowCount = filteredTableData.count;
else
    rowCount = allTableData.count;

return rowCount;
 }

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

}
return self;
}

Upvotes: 0

Views: 126

Answers (3)

mskw
mskw

Reputation: 10308

Make sure cellForRowAtIndexPath is actually being called (use NSlog or breakpoint to check), if not, you have not set the UITableViewController's tableView to the tableView it is controlling, or you have not set your dataSource Delegate.

Upvotes: 0

Marcos Crispino
Marcos Crispino

Reputation: 8218

You need to set the tableView's delegate.

From Apple's documentation:

A UITableView object must have an object that acts as a data source and an object that acts as a delegate

Upvotes: 0

Dmitry Shevchenko
Dmitry Shevchenko

Reputation: 32394

Looks like you never assign your table view to your controller, add

self.tableView = tableView

as a second line in viewDidLoad

Upvotes: 1

Related Questions