iOS_UITableView
2015-07-14 本文已影响2865人
738bc070cd74
- 实现协议
@interface FoodsViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
- 添加到view
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 44)];
searchBar.placeholder = @"搜索";
searchBar.delegate = self;
menuTableView = [[UITableView alloc] initWithFrame:screenRect style:UITableViewStylePlain];
menuTableView.dataSource = self;
menuTableView.rowHeight = 350;
menuTableView.delegate = self;
menuTableView.tableHeaderView = searchBar;
[self.view addSubview:menuTableView];
- 实现委托方法
//cell 的数量
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return menus.count;
}
// 分组的数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// 具体cell的返回
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImageView *imageview;
UILabel *nameLabel;
UILabel *contentLabel;
static NSString *cellIndetifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetifier];
imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(110, 0, 200, 30)];
contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 110, screenWidth, 200)];
[contentLabel setNumberOfLines:0];
[cell addSubview:imageview];
[cell addSubview:nameLabel];
[cell addSubview:contentLabel];
}
NSString *content = [menus[indexPath.row] objectForKey:@"content"];
NSString *des = [menus[indexPath.row] objectForKey:@"description"];
NSString *img = [menus[indexPath.row] objectForKey:@"img"];
NSString *name = [menus[indexPath.row] objectForKey:@"name"];
NSLog(@"%@, %@, %@, %@", content,des,img,name);
[imageview sd_setImageWithURL:[NSURL URLWithString:[@"http://www.yi18.net/" stringByAppendingString:img]]];
contentLabel.text = content;
nameLabel.text = name;
return cell;
}
// 选中单个cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailsViewController *detailsVC = [[DetailsViewController alloc] init];
detailsVC.dic = [shots objectAtIndex:indexPath.row];
[self presentViewController:detailsVC animated:YES completion:nil];
}
- 添加在顶部的SearcherBar
实现协议UISearchBarDelegate
//回调代理方法
- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSString *editString = searchBar.text;
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
NSString *searchUrl = @"http://apis.baidu.com/yi18/menu/search";
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[dic setObject:@"1" forKey:@"page"];
[dic setObject:@"20" forKey:@"limit"];
[dic setObject:editString forKey:@"keyword"];
[HttpUtils.shareInstance GET:searchUrl
apikey:@"bcab4059a164b3c0c0f515c96d94b884"
parameters:dic
success:^(AFHTTPRequestOperation *operation, id responseObject) {
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
NSLog(@"success %@", responseObject);
NSMutableArray *temp = [responseObject objectForKey:@"yi18"];
menus = temp;
[menuTableView reloadData];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error %@", error);
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}
];
}