繁体   English   中英

对节标题Tableview iOS的操作

[英]Action on Section Header Tableview iOS

我在节标题中有一个视图,在其中放置了一个Button 在该按钮上,单击API命中,但选择的节索引错误,我想在“节头”索引处将Object发送为参数。

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

您将变量indexx保留为全局变量,因此,如果我们滚动tableView,则该变量将得到更新。 因此,这不是一个好方法。 请更新您的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);
}

做到这一点。

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

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM