我在节标题中有一个视图,在其中放置了Button。在该按钮上,点击了API,但选择的部分索引错误,
我想在节头索引处发送对象作为参数。

这是我的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);
}

最佳答案

做这个。

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

    }

关于ios - 对节标题Uitableview iOS的操作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41499979/

10-10 19:15