[英]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. 我正在尝试实用地创建一个表格视图(没有xib,也没有情节提要),但是食物数组根本不在表格视图中显示。 I will post all my code in view controller. 我将所有代码发布到视图控制器中。
Viewcontroller.h 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 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;
}
Looks like you never assign your table view to your controller, add 看起来您从未将表视图分配给控制器,添加
self.tableView = tableView
as a second line in viewDidLoad
作为viewDidLoad
的第二行
You need to set the tableView's delegate. 您需要设置tableView的委托。
From Apple's documentation : 从Apple的文档中 :
A UITableView object must have an object that acts as a data source and an object that acts as a delegate UITableView对象必须具有充当数据源的对象和充当委托的对象
确保cellForRowAtIndexPath实际上被调用(使用NSlog或断点检查),否则,您尚未将UITableViewController的tableView设置为它正在控制的tableView,或者尚未设置dataSource委托。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.