为什么在重用标头上调用tableview标头initWithCoder方法

时间:2014-04-06 07:09:03

标签: ios uitableview

我有一个带标题的tableview

现在我注册了这个标题的笔尖:

[tableView registerNib:[UINib nibWithNibName:[self layoutName] bundle:[NSBundle mainBundle]] forHeaderFooterViewReuseIdentifier:[self cellIdentifier]];
在tableview控制器中

和这一行

MyCustomHeader *header = (MyCustomHeader*)[tableView dequeueReusableHeaderFooterViewWithIdentifier:[self cellIdentifier]];

始终返回非空单元格

我给表头的笔尖,有几个自定义对象 致电dequeueReusableHeaderFooterViewWithReuseIdentifier

之后

自定义对象的initWithCoder似乎被称为

为什么这样,如果该单元应该被重用,并且所有对象都已经“先前设置”了?

编辑打开/关闭代码

- (void)sectionHeaderView:(BaseSectionHeaderView *)sectionHeaderView sectionOpened:(NSInteger)sectionOpened {
    LogInfo(@"opening section:%ld",sectionOpened);
    if(self.openSection.openSectionHeaderView == sectionHeaderView){
     [self sectionHeaderView:sectionHeaderView sectionClosed:sectionOpened];
        LogInfo(@"this section is already open");
        return;
    }
    [_purchaseDataSource setSectionAtIndex:sectionOpened Open:YES];

    /*
     Create an array containing the index paths of the rows to insert: These correspond to the rows for each quotation in the current section.
    */
    NSInteger countOfRowsToInsert = 1;
    NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
   for (NSInteger i = 0; i < countOfRowsToInsert; i++) {
        [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:sectionOpened]];
    }

    /*
     Create an array containing the index paths of the rows to delete: These correspond to the rows for each quotation in the previously-open section, if there was one.
     */
    NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];

   if (self.openSection.openSectionHeaderView != nil) {

        [_purchaseDataSource setSectionAtIndex:self.openSection.openSectionIndex Open:NO];
       [_openSection.openSectionHeaderView toggleOpenWithUserAction:NO];
       NSInteger countOfRowsToDelete = [_purchaseInvoiceListTable.tableView numberOfRowsInSection:self.openSection.openSectionIndex];        for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
            [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:self.openSection.openSectionIndex]];
        }

   }

    // style the animation so that there's a smooth flow in either direction
    UITableViewRowAnimation insertAnimation;
    UITableViewRowAnimation deleteAnimation;
    if (self.openSection.openSectionHeaderView == nil || sectionOpened < self.openSection.openSectionIndex) {
        insertAnimation = UITableViewRowAnimationTop;
        deleteAnimation = UITableViewRowAnimationBottom;
    }
    else {
        insertAnimation = UITableViewRowAnimationBottom;
        deleteAnimation = UITableViewRowAnimationTop;
    }


        // apply the updates
        [_purchaseInvoiceListTable.tableView beginUpdates];
        [_purchaseInvoiceListTable.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:insertAnimation];
        [_purchaseInvoiceListTable.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:deleteAnimation];
        [_purchaseInvoiceListTable.tableView endUpdates];

    CGRect frame = sectionHeaderView.frame;
    LogInfo(@"frame: %@",[NSValue valueWithCGRect:frame]);
    [self scrollTableViewFromBottomAnimated:YES];

        //
        self.openSection.openSectionIndex = sectionOpened;
        self.openSection.openSectionHeaderView = sectionHeaderView;
    self.openSection.sectionHeight = [NSNumber numberWithFloat:sectionHeaderView.frame.size.height];
        LogInfo(@"sectionOpened:%ld",sectionOpened);





}





- (void)sectionHeaderView:(BaseSectionHeaderView *)sectionHeaderView sectionClosed:(NSInteger)sectionClosed {

    /*
     Create an array of the index paths of the rows in the section that was closed, then delete those rows from the table view.
     */
       [_purchaseDataSource setSectionAtIndex:sectionClosed Open:NO];
    NSInteger countOfRowsToDelete = [_purchaseInvoiceListTable.tableView numberOfRowsInSection:sectionClosed];

    if (countOfRowsToDelete > 0) {
        NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
            [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:sectionClosed]];
        }
        [_purchaseInvoiceListTable.tableView beginUpdates];
        [_purchaseInvoiceListTable.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationTop];
        [_purchaseInvoiceListTable.tableView endUpdates];


    }

    self.openSection.openSectionIndex = NSNotFound;
    self.openSection.openSectionHeaderView = nil;
    LogInfo(@"sectionClosed:%ld",sectionClosed);
}



- (void)toggleOpenWithUserAction:(BOOL)userAction {

    // toggle the disclosure button state
    self.selected = !self.selected;

    // if this was a user action, send the delegate the appropriate message
    if (userAction) {
        if (self.selected) {
            if ([self.delegate respondsToSelector:@selector(sectionHeaderView:sectionOpened:)]) {
                [self.delegate sectionHeaderView:self sectionOpened:self.section];
            }
        }
        else {

            if ([self.delegate respondsToSelector:@selector(sectionHeaderView:sectionClosed:)]) {
                [self.delegate sectionHeaderView:self sectionClosed:self.section];
            }
        }
    }

}

0 个答案:

没有答案