iphone - TableView 无法正常工作。

标签 iphone ios objective-c ipad uitableview

我正在尝试以务实的方式创建一个表格 View (没有xib,也没有 Storyboard),但是食物数组根本没有显示在表格 View 中。我将在 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;
}

最佳答案

看起来您从未将表格 View 分配给 Controller ,请添加

self.tableView = tableView

作为viewDidLoad中的第二行

关于iphone - TableView 无法正常工作。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15886748/

相关文章:

iphone - UIDatePicker 不显示正确的时间

iphone - 从 View 1 切换到蓝牙设置......不可能吗?

ios - 推送通知列表

ios - 两个应用程序使用相同的显式应用程序 ID

ios uitableview 反弹出错

iphone - Objective C,滚动菜单缩略图

ios - AppDelegate 函数未加载 TableView Controller

ios - 带圆角的 UIView : how to clip subviews correctly?

c++ - 位字段类型是否需要相同?

ios - 具有不同高度的垂直对齐的 Collection View 单元格