2010-11-12 77 views
2

我有一個帶有自定義標題的表格。除此之外,我還實現了一個摺疊功能,以便在觸摸標題時,所有未加標題的部分都會縮小,並且擴展了所要的部分。 我意識到通過使用UIButtons作爲背景視圖。此外,按鈕具有不同的顏色和文本,具體取決於它們的狀態(擴展與不擴展)。uitableview中的section header - reusidentifier?

我有一個問題,類似於reusidentifier問題,即如果你不重用已經在表中分配的單元格,某些片段出現,如果你開始滾動。在這裏它只發生在我的第一部分,但它似乎支離破碎和重複....

有沒有什麼類似於重新使用已經分配一次像UITableViewCells方法reusIdentifier的headerviews?

還是有更好的方法?

分配按鍵提前將它們存儲在陣列沒有做的伎倆我...

下面是一些代碼:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 40)]; 
[button setShowsTouchWhenHighlighted:NO]; 
[button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(collapse:) forControlEvents:UIControlEventTouchUpInside]; 
button.tag = section; 

[button setAdjustsImageWhenHighlighted:NO]; 
button.backgroundColor = [UIColor colorWithRed:63.0f/255.0f green:154/255.0f blue:201/255.0f alpha:1.0f]; 


[button.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:14.0]]; 
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; 
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter]; 
if (section == 0) { 
// [button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; 
[button setTitle:@" Newsroom" forState:UIControlStateNormal]; 
} else { 
    [button setTitle:[NSString stringWithFormat:@" Sektion %d", section] forState:UIControlStateNormal]; 

} 


if (section == mySection) { 
    [button setBackgroundImage:[UIImage imageNamed:@"sectionHeader_active.png"] forState:UIControlStateNormal]; 
    [button setBackgroundImage:[UIImage imageNamed:@"sectionHeader_active.png"] forState:UIControlEventTouchUpInside]; 
    [button.titleLabel setTextColor:[UIColor whiteColor]]; 

} else { 
    [button setBackgroundImage:[UIImage imageNamed:@"sectionHeader_inactive.png"] forState:UIControlStateNormal]; 
    [button setBackgroundImage:[UIImage imageNamed:@"sectionHeader_inactive.png"] forState:UIControlEventTouchUpInside]; 

    [button.titleLabel setTextColor:[UIColor colorWithRed:96.0f/255.0f green:96/255.0f blue:97/255.0f alpha:1.0f]]; 

} 

return [button autorelease]; 
} 

回答

0

我也用的UIButton標題意見實施了塌陷部分表。我發現這是愚蠢地試圖緩存和重新使用視圖,因爲你不知道UITableView何時完成(我崩潰了)。

我建議通過以下方式實現您的狀態更改:更新數據模型;執行所有的行插入/刪除以執行摺疊/展開;然後做一個[tableView reloadData],它將調用viewForHeaderInSection,在這裏你使用你的數據模型的狀態來返回適當格式的uibutton視圖。

相關問題