ButtonView中的Button标签相同

时间:2016-10-19 04:30:11

标签: objective-c

我为标题添加了视图,这是我的代码:

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *view = [[UIView alloc] init];
    view.layer.borderWidth = 0.5;
    view.layer.borderColor = [UIColor colorWithRed:127.0/255.0 green:229.0/255.0 blue:232.0/255.0 alpha:1.0].CGColor;
    view.backgroundColor = [UIColor clearColor];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 150, 50)];
    label.text = sectionArray[section];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];

    button = [[UIButton alloc] initWithFrame:CGRectMake(self.myTableView.frame.size.width - 50, 15, 20, 20)];
    [button setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
    button.tag = section;
    [button addTarget:self action:@selector(extentFunction) forControlEvents:UIControlEventTouchUpInside];

    [view addSubview:label];
    [view addSubview:button];
    return view;
}

-(void)extentFunction {

    switch (button.tag) {
        case 0:
            NSLog(@"0");
            break;
        case 1:
            NSLog(@"1");
            break;
        case 2:
            NSLog(@"2");
            break;
        case 3:
            NSLog(@"3");
            break;
        default:
            break;
    }
}

问题是:按钮的标签与每个标题相同,而不是部分整数(例如:第一个标题按钮的标记为0;第二个标题按钮的标记为1)我想要。

我该怎么办?

2 个答案:

答案 0 :(得分:0)

首先看一下AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer = [AFHTTPRequestSerializer serializer]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; NSURLSessionDataTask *dataTask = [manager dataTaskWithHTTPMethod:@"HEAD" URLString:URLString parameters:parameters headerFields:headerFields success:^(NSURLSessionDataTask *task, __unused id responseObject) { if (success) { success(task); // on success block you can fire your final API with below refresh token [manager.requestSerializer setValue:@"Bearer RRFRESH_TOKEN" forHTTPHeaderField:@"Authorization"]; } } failure:failure]; [dataTask resume]; &的明确问题。 local变量。

您需要将global定义为本地变量,并使用button操作发件人作为按钮对象,并从该按钮获取标记。

请查看以下代码我认为它可以帮助您了解您的问题:

button

让我知道它是否正常工作。

答案 1 :(得分:0)

这是您的自定义按钮代码,这是正确的。

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.myTableView.frame.size.width - 50, 15, 20, 20)];
[button setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
button.tag = section;
[button addTarget:self action:@selector(extentFunction) forControlEvents:UIControlEventTouchUpInside];

但您还需要添加其发件人和事件以获取按钮的触摸位置,如下面的代码。

 [button addTarget:self action:@selector(extentFunction:event:) forControlEvents:UIControlEventTouchUpInside];

现在,当您点击按钮时,将调用extentFunction选择器。现在你所要做的就是实现你的按钮选择器,如下面的代码。

-(void)extentFunction:(id)sender event:(id)event{
  NSSet *touches = [event allTouches];
  UITouch *touch = [touches anyObject];

  CGPoint currentTouchPosition = [touch locationInView:_yourTableViewName];
  NSIndexPath *indexPath = [_yourTableViewName indexPathForRowAtPoint: currentTouchPosition];
  NSLog(@"%ld",indexPath.section);
}

希望这会帮助你。