Пользовательская проблема TableView - PullRequest
0 голосов
/ 20 марта 2012

У меня есть собственное представление заголовка в моем приложении для iphone. и у него есть пользовательская кнопка над этим. Но это пользовательское действие кнопки не запускается, а вызывается событие нажатия на заголовок, которое находится в пользовательском классе. Пожалуйста, помогите мне. Как запустить действие для кнопки в представлении заголовка.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSNumber *num=[NSNumber numberWithInt:section];
    UIView * customView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,320,50)]autorelease];
    customView.tag = section;
    [customView setUserInteractionEnabled:YES];
    customView.backgroundColor = [UIColor blackColor];

    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(260, 7, 25, 25);
    button.tag=section;
    [button addTarget:self action:@selector(expandCollapsing:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageNamed:@"add.png"] forState:UIControlEventTouchUpInside];
    [button setSelected:YES];

}

Ответы [ 2 ]

0 голосов
/ 20 марта 2012

Просто вставьте этот код, чтобы решить вашу проблему:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{   
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(50.0, 0.0, 25, 25);
    [button setImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal];
    button.tag=section;
    [button addTarget:self action:@selector(expandCollapsing:) forControlEvents:UIControlEventTouchUpInside];
    [button setSelected:YES];
    return button;

}
-(void)expandCollapsing:(id)sender {
    UIButton *btn=(UIButton*)sender;
    NSLog(@"Tag:%d",[btn tag]);
}
0 голосов
/ 20 марта 2012

Вы можете реализовать свой собственный метод обратного вызова, чтобы сделать это.

Что я сделал, так это Я добавил TapGesture в customView. Когда событие вызывается, я использовал метод обратного вызова, чтобы передать элемент управления в viewController, где реализован мой tableView

Что вы можете сделать При загрузке заголовка вы можете присвоить значение тега для customView

customHeader.tag = 1;

и вы можете реализовать метод обратного вызова в вашем viewController.

В CustomheaderView

 UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkBoxEventHandler:)];
  [recognizer setNumberOfTapsRequired:1];
  [recognizer setNumberOfTouchesRequired:1];
  [self setGestureRecognizers:[NSArray arrayWithObject:recognizer]];


-(IBAction)checkBoxEventHandler:(id)sender
{
     if ([self.delegate respondsToSelector:@selector(selectedHeader: atIndex:)]) 
     {
            [self.delegate selectedHeader:self atIndex:self.tag];
     }
}

In your viewController.m
-(void)selectedHeader:(myCustomHeader *)header atIndex:(int)index
{
   //header -> will give you the same instance you created in your viewController while loading this custom header view
   // So you can check the tag like this
   if(header.tag == 1) 
   { 
      //Do Stuff here
   }

   // index -> we are passing headerViews tag as index ie header.tag is equal to index
   // So checking the tag like this is the proper way
   if(index == 1) 
   { 
      //Do Stuff here
   }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...