簡體   English   中英

刪除行時添加到自定義節標題視圖的按鈕消失

[英]Button added to custom section header view disappears when row is deleted

剛在我的應用中遇到了一些非常奇怪的行為。 我已經在最簡單的情況下重新創建了該問題:

NSMutableArray *data;

- (void)viewDidLoad {
    [super viewDidLoad];
    data = [[NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil] retain];
}

- (UIView *)tableView:(UITableView *)aTableView viewForHeaderInSection:(NSInteger)section { 
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 32.0)];
    header.backgroundColor = [UIColor lightGrayColor];
    [header addSubview:self.button];
    return header;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {    
        [data removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
    cell.textLabel.text = [data objectAtIndex:indexPath.row];
    return cell;
}

- (void)dealloc {
    [super dealloc];
}

每次我刪除一行; 我標題中的按鈕消失了! 無論我使用哪種類型的rowAnimation,都會發生這種情況。 如果我向上滾動表格,以便標題向下滾動; 當標題返回時,按鈕返回。 該按鈕在xib文件中創建。

我可以通過以下兩種方法之一解決它:

  • 刪除后重新加載tableView數據; 延遲,以便刪除動畫首先完成。
  • 在viewForHeaderInSection中而不是在interfaceBuilder中創建按鈕。

    我真的很想了解這里發生了什么。 按鈕在哪里? 我已經確認刪除行時會調用viewForHeaderInSection。

編輯我嘗試更改它,以便在viewForHeader中而不是在xib中創建按鈕,但是這會引起其他奇怪的問題...當我創建或刪除按鈕時,我正在設置某些屬性,例如標題和啟用情況,具體取決於表格中有多少個項目。 當我刪除表格的最后一行時,直到我將按鈕滾動離開屏幕然后再次打開,我才能看到文本和啟用狀態的更新。

因為您只有一個按鈕實例,所以如果表視圖決定創建一個新的標題視圖,則該按鈕將從其當前父視圖中刪除,然后移至新的視圖。 即使表中只有一個部分,表視圖也可能在內部做一些奇怪的事情,並在屏幕外重新創建標題視圖,因此您不能一次只依靠一個存在的表。

應該viewForHeaderInSection:創建按鈕,並解決其他問題。 您不僅應該更新viewForHeaderInSection中的按鈕屬性,還應該處理所有刪除事件,以便刪除行也將更新按鈕。

委托方法tableView:heightForHeaderInSection:實現在哪里? 這對於tableView:viewForHeaderInSection:正常工作是必要的。 檢查文檔。 UITableView委托的參考

我已經確認刪除行時會調用viewForHeaderInSection。

您是否已確認使用添加的按鈕為特定標題調用了viewForHeaderInSection? 然后,嘗試添加

[header bringSubviewToFront:self.button];  

添加按鈕后。

好吧,我至少設法解決了這個問題...我為我在viewForheaderAtSection創建的視圖創建了一個iVar和屬性,然后僅當我還沒有一個視圖時才創建一個新視圖。 否則,我只返回我已經擁有的標題; 像這樣的東西:

- (UIView *)tableView:(UITableView *)aTableView viewForHeaderInSection:(NSInteger)section { 
    if (!self.myHeader){
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 32.0)];
        header.backgroundColor = [UIColor lightGrayColor];
        [header addSubview:self.button];
        self.myHeader = header;
        [header release];
    }
    return self.myHeader;
}

這行得通,但是了解到底發生了什么仍然很棒。 據我所知,系統正在調用viewForHeaderInSection,但是實際上我未使用該方法返回的視圖實例。 至少直到我做一些導致視圖重繪的事情為止...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM