iOS_UITableView

  • 實現(xiàn)協(xié)議
@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];
  • 實現(xiàn)委托方法

//cell 的數(shù)量

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return menus.count;
}

// 分組的數(shù)量

-(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
    實現(xiàn)協(xié)議UISearchBarDelegate

//回調(diào)代理方法
- (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];
                     }
 ];

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容