我在Section Header中有一个视图,我在其上放置了error_reporting(0);
。在该按钮上单击API命中但选择的部分索引是错误的,
我想在Section Header索引中将Object作为参数发送。
以下是Button
的代码:
Tableview
答案 0 :(得分:1)
您将变量indexx保留为全局变量,因此如果我们滚动tableView,则变量get update。所以这不是一个好方法。请更新您提供的2种方法,如下所述
方法:1
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *aView =[[UIView alloc] initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, 40)];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setTag:section+1];
[aView addSubview:btn];
[btn addTarget:self action:@selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(btn.frame.origin.x+20, btn.frame.origin.y, btn.frame.size.width, btn.frame.size.height)];
title.text=[[DATAA objectAtIndex:section] objectForKey:@"subitemname"];
title.font=[UIFont boldSystemFontOfSize:12.0];
title.textColor=[UIColor grayColor];
[aView addSubview:title];
return aView;
}
方法2:
- (void)sectionTapped:(UIButton*)btn {
indexx=[[DATAA objectAtIndex:btn.tag - 1] objectForKey:@"subitemid"];
NSString *urlString = [NSString stringWithFormat:@"http://dealnxt.com/api/SearchItem?subitemid=%@",indexx];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
NSLog(@"json:%@",jsonDict);
}
答案 1 :(得分:1)
这样做。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [[DATAA objectAtIndex:section] objectForKey:@"subitemname"];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *aView =[[UIView alloc] initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, 40)];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setTag:section+1];
[aView addSubview:btn];
[btn addTarget:self action:@selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(btn.frame.origin.x+20, btn.frame.origin.y, btn.frame.size.width, btn.frame.size.height)];
title.text=[[DATAA objectAtIndex:section] objectForKey:@"subitemname"];
title.font=[UIFont boldSystemFontOfSize:12.0];
title.textColor=[UIColor grayColor];
[aView addSubview:title];
//indexx=[[DATAA objectAtIndex:section] objectForKey:@"subitemid"];
//NSLog(@"indexL:%@",indexx);
return aView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [DATAA count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat height = 30;
return height;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *subMenuData = [[DATAA objectAtIndex:section] objectForKey:@"Submenu"];
return [subMenuData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell=[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSDictionary *cellData = [[[DATAA objectAtIndex:indexPath.section] objectForKey:@"Submenu"] objectAtIndex:indexPath.row];
cell.textLabel.text=[cellData objectForKey:@"subtosubitemname"];
cell.textLabel.font=[UIFont boldSystemFontOfSize:12.0];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (void)sectionTapped:(UIButton*)btn {
NSString *urlString = [NSString stringWithFormat:@"http://dealnxt.com/api/SearchItem?subitemid=%@",btn,tag];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
NSLog(@"json:%@",jsonDict);
}