簡體   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