UITableViewカスタムセクションヘッダー、重複問題
-
23-09-2019 - |
質問
私はトラブルカスタムのUITableViewのセクションヘッダのアニメーションを持っています。
目標は、折りたたみ式のセクションを作成することでした。
Iカスタムヘッダをタップすると、期待どおり、それはアニメーション初めて、しかしその後たびに、それは元の場所に複製を残し、他をアニメーション
画像例:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* customView = [[[UIView alloc]initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)]autorelease];
customView.backgroundColor = [UIColor lightGrayColor];
UILabel * headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor darkGrayColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
headerLabel.frame = CGRectMake(10, 7, 260.0, 44.0);
headerLabel.textAlignment = UITextAlignmentCenter;
NSDictionary *dictionary = [self.data objectAtIndex:section];
headerLabel.text = [dictionary objectForKey:@"Title"];
[customView addSubview:headerLabel];
// add button to right corner of section
UIButton* headerButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
headerButton.center = CGPointMake( 160.0, 22.0);
headerButton.backgroundColor = [UIColor clearColor];
headerButton.tag = section;
[headerButton addTarget:self action:@selector(expandSection:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:headerButton];
return customView;
}
私のアニメーション方法:
- (void) expandSection:(id)sender {
if (expandedSection == [sender tag]) {
expandedSection = -1;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
}else if (expandedSection == -1){
expandedSection = [sender tag];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
}else{
[self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:expandedSection] withRowAnimation:UITableViewRowAnimationNone];
expandedSection = [sender tag];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
//[self.tableView reloadData];
}
私は上に行く正確にわからないいただきましたけど、インスタンスは、私はdealoc何かに必要なことを示唆しています。私はいくつかのことを試してみたが、私はこれを把握することはできません。この上の誰も助けは素晴らしいことだ!
編集:私はこの問題は、reloadSectionsがインスタンスにカスタムビューを引き起こしているということであると信じています。私はアニメーションのためのアップデートを行うための基準として、それを必要とするので、私は、ビューを解放することはできません。私はこれを修正するために何ができるか上の任意のアイデア?
解決
ソリューションを見つけます。
すべての変更の前に再ロードするために必要なテーブル。この方法で表が変更を行う前に、最新の状態である。
を追加 【self.tableView reloadData]。 「expandSection」方式での拳のエントリとしてます。
CODEます:
- (void) expandSection:(id)sender {
[self.tableView reloadData];
if (expandedSection == [sender tag]) {
expandedSection = -1;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
}else if (expandedSection == -1){
expandedSection = [sender tag];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
}else{
[self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:expandedSection] withRowAnimation:UITableViewRowAnimationNone];
expandedSection = [sender tag];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
//[self.tableView reloadData];
}
他のヒント
私は、ダイナミックな高さの細胞を使用することによって引き起こされた同様の問題がありました。私は(彼らはそれぞれ崩壊、拡大されたことを意味する)セクションの関連する行を挿入し、削除するのtableViewを更新したとき、私はUITableViewHeaderFooterView
のサブクラスたセクションヘッダがリサイクルされていなかった、拡張可能なカスタムヘッダビューを持っていました。だから、基本的に新しいものが割り当てられるとビューを重ね、その結果、古いものの上に追加されました。適切になるように設定された識別子のセルは、何か他のものとなっている必要があります。 IはtableView.sectionHeaderHeight = UITableViewAutomaticDimension
、実施func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
を除去する際にビューが適切にリサイクルさ持って、唯一のヘッダビューは、セクションごとに表示されていた。
別の解決策私は実際に仕事ではなくUITableViewCell
のUITableViewHeaderFooterView
を使用していた、あなたがfunc tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
であなただけのreturn cell.contenView
を返すとき、このメソッドは、UIViewのを返すために必要なので、仕事とのUITableViewCellのcontentView
以来のUIViewであるだろうが判明していることそれがうまく動作します。アイデアの背後には、UITableViewCellの経由のUITableViewのリサイクルメカニズムを使用して、あなたがそれを設定した後だけにその内容を返すことです。
結論。セルフサイジングのtableView細胞およびUITableViewHeaderFooterView
代わりに手動でセルの高さを算出して使用する場合の問題は、それがUITableViewAutomaticDimension
によって引き起こされることは非常に可能である。