我正在嘗試創建一個實用的table view(沒有xib和storyboards),但食物數組並沒有在表格視圖中顯示出來。我將在視圖控制器中發佈我的所有代碼。表視圖無法正常工作。
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;
}
OMG我加了它,但沒有在右邊線LOL。您將獲得該複選標記,只需等待10分鐘。 – moomoo 2013-04-08 18:59:47