UITableView的自定义节头,重复的问题
-
23-09-2019 - |
题
我无法动画自定义的UITableView节头。点击 我们的目标是创造可折叠部分。点击 当我在自定义首部点击第一次动画如预期,但是每一次之后,它让在原来的位置重复和动画另一个。
图像示例:
- (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
的一个子类并没有再循环。所以基本上一个新被分配并添加了旧造成重叠的看法。标识被正确设定这样的细胞一定是别的东西。当我除去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
引起的。