0

我正在尝试务实地创建一个表格视图(没有 xib 也没有情节提要),但是食物数组根本没有出现在表格视图中。我将在视图控制器中发布我的所有代码。

视图控制器.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;

视图控制器.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;
}
4

3 回答 3

1

看起来你从来没有将你的表格视图分配给你的控制器,添加

self.tableView = tableView

作为第二行viewDidLoad

于 2013-04-08T18:56:01.067 回答
0

您需要设置 tableView 的委托。

来自苹果的文档

UITableView 对象必须有一个充当数据源的对象和一个充当委托的对象

于 2013-04-08T19:02:58.537 回答
0

确保 cellForRowAtIndexPath 实际被调用(使用 NSlog 或断点检查),如果没有,则您尚未将 UITableViewController 的 tableView 设置为它正在控制的 tableView,或者您尚未设置 dataSource Delegate。

于 2013-04-08T19:39:36.517 回答