Как я могу обновить/перезагрузить/обновить представление заголовка раздела в UITableView?

Я хочу изменить определенный вид заголовка моего UITableView, когда я нажимаю строку.

Я прочитал все сообщения об этом. Я пробовал "reloadData", "setNeedDisplay", "reloadSections :withRowAnimation :" и несколько других идей... делать нечего. Мое представление заголовка либо не обновляется, либо делает странные вещи, такие как обновление, только когда я перемещаю представление таблицы (, чего я не хочу достичь ).

Мой код пока выглядит так (в отношении методов делегатов UITableView):

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    if(tableView==_storeTableView){
        return [_storeDataArray count];
    } else {
        return 1;
    }
}

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {
    if(tableView==_storeTableView){
        HouraStoreHeaderModel *headerModel = [self.headerInfoArray objectAtIndex:section];
        if (!headerModel.headerView) {
            NSString *shelfName = headerModel.shelf;
            headerModel.headerView = [[[HouraStoreHeaderView alloc] initWithFrame:CGRectMake(0.0, 0.0, _storeTableView.bounds.size.width, 80) title:shelfName section:section subheaderNumber:([headerModel.openedSubHeaders count]-1) delegate:self] autorelease];
        }
        return headerModel.headerView;
    } else {
        return nil;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableView==_storeTableView){
        HouraStoreHeaderModel *headerModel = [self.headerInfoArray objectAtIndex:section];
        NSDictionary *myDict = _storeDataDict;
        for (NSInteger i = 0; i < [headerModel.openedSubHeaders count]; i++) {
            myDict = [myDict objectForKey:[headerModel.openedSubHeaders objectAtIndex:i]];
        }
        NSInteger numberOfRowsInSection = [[myDict allKeys] count];
        return headerModel.open ? numberOfRowsInSection : 0;
    } else if(tableView==_searchTableView){
        return [_resultArray count];
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    if(tableView==_storeTableView){
        HouraStoreHeaderModel *headerModel = [self.headerInfoArray objectAtIndex:indexPath.section];
        NSDictionary *myDict = _storeDataDict;
        for (NSInteger i = 0; i < [headerModel.openedSubHeaders count]; i++) {
            myDict = [myDict objectForKey:[headerModel.openedSubHeaders objectAtIndex:i]];
        }
        cell.accessoryView=[[[HouraStoreCellView alloc] initWithFrame:CGRectMake(0.0, 0.0, _storeTableView.bounds.size.width, 50) title:[[myDict allKeys] objectAtIndex:indexPath.row]] autorelease];
        return cell;
    } else if (tableView==_searchTableView) {
        cell.textLabel.text = [_resultArray objectAtIndex:indexPath.row];
        return cell;
    } else {
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    HouraStoreHeaderModel *headerModel = [self.headerInfoArray objectAtIndex:section];
    NSInteger height = 59.0 + ([headerModel.openedSubHeaders count]-1)*41.0;
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(tableView==_storeTableView){
        HouraStoreHeaderModel *headerModel = [self.headerInfoArray objectAtIndex:indexPath.section];
        NSDictionary *myDict = _storeDataDict;
        for (NSInteger i = 0; i < [headerModel.openedSubHeaders count]; i++) {
            myDict = [myDict objectForKey:[headerModel.openedSubHeaders objectAtIndex:i]];
        }
        if ([[myDict objectForKey:[[myDict allKeys] objectAtIndex:indexPath.row]] isKindOfClass:[NSDictionary class]]) {
            [self cellOpened:indexPath];
        } else {
            [_activityIndicatorView startAnimating];
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(_listProductsFoundedFinished:)
                                                         name:HouraSearchProductsDone
                                                       object:nil]; 
            NSString *searchString = [[myDict allKeys] objectAtIndex:indexPath.row];
            searchString = [searchString stringByReplacingOccurrencesOfString:@"\"" withString:@"\\u0022"];
            [_singleton.util beginSearchProducts:searchString context:@"2"];
        }
    } else if(tableView==_searchTableView){
        _searchBar.text = [_resultArray objectAtIndex:indexPath.row];

        [_searchBar resignFirstResponder];
        [_activityIndicatorView startAnimating];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(_listProductsFoundedFinished:)
                                                     name:HouraSearchProductsDone
                                                   object:nil]; 
        [_singleton.util beginSearchProducts:_searchBar.text context:@"2"];
    }
}

-(void)headerView:(HouraStoreHeaderView*)headerView headerOpened:(NSInteger)headerOpened {
    if (self.openSectionIndex!=NSNotFound) {
        [self closeAllHeaders];
    }
        //[self closeAllHeaders];
    HouraStoreHeaderModel *headerModel =nil;

    headerModel = [self.headerInfoArray objectAtIndex:headerOpened];
    headerModel.open = YES;
    headerModel.headerView.disclosureButton.selected = YES;

    NSDictionary *myDict = _storeDataDict;
    for (NSInteger i = 0; i < [headerModel.openedSubHeaders count]; i++) {
        myDict = [myDict objectForKey:[headerModel.openedSubHeaders objectAtIndex:i]];
    }
    NSInteger countOfRowsToInsert = [[myDict allKeys] count];

    NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < countOfRowsToInsert; i++) {
        [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:headerOpened]];
    }

    NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];

    NSInteger previousOpenSectionIndex = self.openSectionIndex;
    if (previousOpenSectionIndex != NSNotFound) {

        HouraStoreHeaderModel *previousHeaderModel = [self.headerInfoArray objectAtIndex:previousOpenSectionIndex];
        previousHeaderModel.open = NO;
        previousHeaderModel.headerView.disclosureButton.selected = NO;
        [previousHeaderModel.headerView toggleOpenWithUserAction:NO];
        NSInteger countOfRowsToDelete = [[[_storeDataDict objectForKey:previousHeaderModel.shelf ] allKeys] count];
        for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
            [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:previousOpenSectionIndex]];
        }
    }
    UITableViewRowAnimation insertAnimation;
    UITableViewRowAnimation deleteAnimation;
    if (previousOpenSectionIndex == NSNotFound || headerOpened < previousOpenSectionIndex) {
        insertAnimation = UITableViewRowAnimationTop;   
        deleteAnimation = UITableViewRowAnimationBottom;
    } else {
        insertAnimation = UITableViewRowAnimationBottom;
        deleteAnimation = UITableViewRowAnimationTop;
    }

    [_storeTableView beginUpdates];
    [_storeTableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:deleteAnimation];
    [_storeTableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:insertAnimation];
    [_storeTableView endUpdates];

    self.openSectionIndex = headerOpened;
}

-(void)headerView:(HouraStoreHeaderView*)headerView headerClosed:(NSInteger)headerClosed {

    HouraStoreHeaderModel *headerModel = [self.headerInfoArray objectAtIndex:headerClosed];

    headerModel.open = NO;
    headerModel.headerView.disclosureButton.selected = NO;
    [headerModel cleanOpenedSubHeaders];
    [self.headerInfoArray replaceObjectAtIndex:headerClosed withObject:headerModel];

    NSInteger countOfRowsToDelete = [_storeTableView numberOfRowsInSection:headerClosed];

    if (countOfRowsToDelete > 0) {
        NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
            [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:headerClosed]];
        }
        [_storeTableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationTop];
    }
    self.openSectionIndex = NSNotFound;
}

-(void)cellOpened:(NSIndexPath*)indexPath {
    HouraStoreHeaderModel *headerModel = [self.headerInfoArray objectAtIndex:indexPath.section];
    [self headerView:headerModel.headerView headerClosed:indexPath.section];
    [headerModel addOpenedSubHeaders:[[[_storeDataDict objectForKey:headerModel.shelf] allKeys] objectAtIndex:indexPath.row]];
    [self.headerInfoArray replaceObjectAtIndex:indexPath.section withObject:headerModel];
    headerModel = [self.headerInfoArray objectAtIndex:indexPath.section];
    [self headerView:headerModel.headerView headerOpened:indexPath.section];
}

-(void)closeAllHeaders {
    for (NSInteger i = 0; i < [self.headerInfoArray count]; i++) {
        HouraStoreHeaderModel *headerModel = [self.headerInfoArray objectAtIndex:i];
        [self headerView:headerModel.headerView headerClosed:i];
    }
}

Что я хотел бы сделать, так это, когда я щелкаю строку, заголовок раздела обновляется, чтобы он содержал новую кнопку с текстом строки. Затем я удаляю строку и перезагружаю новые данные в строках раздела. Мне удалось отлично справиться со строками. Но я не могу найти способ обновить этот заголовок.

Спасибо за любую идею.

10
задан PeeHaa 12 January 2014 в 03:26
поделиться