Как сделать заголовок раздела доступным в UITable? - PullRequest
2 голосов
/ 05 сентября 2011

Я хочу сделать это как, коснитесь заголовка раздела, а затем перейдите к концу этого раздела.Как я мог это сделать?Какие-либо предложения?Ура

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
    [headerView setBackgroundColor:[[UIColor grayColor] colorWithAlphaComponent:0.7]];
    UILabel *titleInSection = [[[UILabel alloc] initWithFrame:CGRectMake(50, 3, tableView.bounds.size.width/3, 20)] autorelease];
    [titleInSection setBackgroundColor:[[UIColor grayColor] colorWithAlphaComponent:0]];
    [titleInSection setTextColor:[UIColor whiteColor]];
    if (isSectionLoad == YES){
        [categoryData retain];
        titleInSection.text = [categoryData objectAtIndex:section];
    }

    titleInSection.tag = section;
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionTapped:)];
    [titleInSection addGestureRecognizer:recognizer];
    [recognizer release];

    [headerView addSubview:titleInSection];
    return headerView;
}

- (IBAction)sectionTapped:(UITapGestureRecognizer *)recognizer
{
    switch (recognizer.view.tag) {
        case 0:
            // do what you want for section with index 0
            NSLog(@"HELLO WORLD!!!");
            break;

        default:
            break;
    }
}

Ответы [ 2 ]

4 голосов
/ 05 сентября 2011

Реализуйте метод - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section в вашем делегате. В этом методе создайте UIView с необходимыми подпредставлениями (метками, изображениями и т. Д.). В конце просто добавьте UITapGestureRecognizer к этому виду. А потом верни его.

Например:

- (IBAction)sectionTapped:(UITapGestureRecognizer *)recognizer
{
    switch (recognizer.view.tag) {
        case 0:
            // do what you want for section with index 0
            break;

        default:
            break;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label;
    label.text = [NSString stringWithFormat:@"Section %d", section];
    label.tag = section;
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionTapped:)];
    [label addGestureRecognizer:recognizer];
    [recognizer release];
    return  label;
}
0 голосов
/ 25 мая 2015

Это старый вопрос, но есть более лучший ответ на этот вопрос:

Смотрите этот ответ: https://stackoverflow.com/a/19173639/2371425

tl; dr - iOS6 предоставил нам эти новые методы для изменения атрибутов верхних / нижних колонтитулов раздела.

-(void)tableView:(UITableView *)tableView 
    willDisplayHeaderView:(UIView *)view 
    forSection:(NSInteger)

- (void)tableView:(UITableView *)tableView
    willDisplayFooterView:(UIView *)view 
    forSection:(NSInteger)section

Используйте эти методы для добавления UITapGestureRecognizer в headerView.

Ex.

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{ 
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionTapped:)];

    [view addGestureRecognizer:recognizer];

}
- (IBAction)sectionTapped:(UITapGestureRecognizer *)recognizer {
 NSLog(@"Tapped");
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...