2017-01-06 22 views
0

セクションヘッダーには、Buttonを配置したビューがあります。そのボタンでAPIヒットしましたが、選択されたセクションインデックスが間違っています オブジェクトをセクションヘッダーインデックスでパラメータとして送信したいとします。ここでセクションヘッダーテーブルビューでの処理iOS

Tableviewのための私のコードです:

- (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://URL/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);  
} 
+0

代わりにグローバル変数として 'indexx'を使用しての、'メソッド でボタンタグをフェッチ - (ボイド)sectionTapped:(UIButton *) ' は、オブジェクトを持参し、あなたのURLに追加BTN。 –

答えて

1

次のように行う

方法以下に述べるように、あなたの2つの方法でアップデートしてください。

- (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); 

    } 
1

我々はのtableViewをスクロールした場合、その変数が更新を得るようにするには、グローバル変数として変数indexxを維持しています。それは良いアプローチではありません。方法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; 
} 

- (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); 
} 
関連する問題