我有UITableView
有4个部分。其中三个部分有一个标题视图。
标题视图是UITableViewCell
,我将其作为viewForHeaderInSection:
代理中的普通单元格进行排队。
如果我从任何部分插入或删除行,则其他tableview
标题单元格将消失。
我认为这与细胞重用有关,但细胞最初都出现在屏幕上(所有三个标题同时出现在屏幕上)。
我在插入或删除后尝试重新加载其他部分,但这没有帮助。
这里有一些代码:
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
switch (section) {
case kSectionCardInformation:
case kSectionContactInformation:
case kSectionTags: {
static NSString *CellIdentifier = @"EditContactHeaderCell";
EditContactHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}
default:
return nil;
}
}
这是我在删除部分中删除行的地方:
- (void)deleteTag:(CDTag *)tag {
[self.tableView beginUpdates];
NSMutableArray *objects = [self.sections objectAtIndex:kSectionTags];
if ([objects containsObject:tag]) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects indexOfObject:tag] inSection:kSectionTags];
[objects removeObject:tag];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
[self.contact deleteTag:tag];
}
[self.tableView endUpdates];
}
任何帮助,非常感谢。
答案 0 :(得分:20)
将UITableViewCell包装成UIView。
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
switch (section) {
case kSectionCardInformation:
case kSectionContactInformation:
case kSectionTags: {
static NSString *CellIdentifier = @"EditContactHeaderCell";
EditContactHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView * view = [[[UIView alloc] init] autorelease];
[view addSubview:cell];
//setup the view's frame: fixed, autolayout, ...
return view;
}
default:
return nil;
}
}
答案 1 :(得分:8)
添加评论,因为我遇到了同样的问题......问题是我也使用UITableViewCell作为标题,而不是使用UITableViewHeaderFooterView。
解决问题:
在viewDidLoad函数中注册此nib
override func viewDidLoad() {
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.register(UINib(nibName: "NibFileName", bundle: nil), forHeaderFooterViewReuseIdentifier: "CustomViewHeaderView")
}
从viewForHeaderInSection函数中取消可重用的HeaderFooterView:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier:"CustomViewHeaderView") as! CustomViewHeaderView
return cell
}
这修复了我消失的标题。我希望它可以帮助别人。
答案 2 :(得分:2)
返回contentView
中的cell
对我来说很好。无需将单元格包装在UIView
中。
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
return cell.contentView
}
答案 3 :(得分:1)
对于Swift 4
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let containerView = UIView()
guard let headerCell = tableView.dequeueReusableCell(withIdentifier: "MyHeaderView") as? MyHeaderView else { fatalError(" Failed to load MyHeaderView") }
containerView.addSubview(headerCell)
return containerView
}
答案 4 :(得分:1)
请勿返回FooterCell或HeaderCell或可重复使用的标识符。返回redirectIdentifier.contentView。对我来说是:返回headerCell!.contentView。 func tableView(_ tableView:UITableView,viewForHeaderInSection部分:Int)-> UIView? { 让headerCell = tableView.dequeueReusableHeaderFooterView(withIdentifier:“ CustomViewHeaderView”)为! CustomViewHeaderView
return headerCell.contentView
}
答案 5 :(得分:0)
更正方法是使用 UITableViewHeaderFooterView ,而不是将 UITableViewCell 用于标题部分。就像@Francois Nadeau回答的那样。
有关如何将 UITableViewHeaderFooterView 用于标题部分的更多详细信息,请参见以下答案:https://stackoverflow.com/a/36931047
答案 6 :(得分:-1)
我不知道你为什么使用tableview单元格作为标题,但我的假设是
您错过了休息声明
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
switch (section) {
case kSectionCardInformation:
break;
case kSectionContactInformation:
break;
case kSectionTags: {
static NSString *CellIdentifier = @"EditContactHeaderCell";
EditContactHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}
default:
return nil;
}
}
在返回单元格之前检查nil
条件,如果它为零,则创建单元格。
static NSString *CellIdentifier = @"EditContactHeaderCell";
EditContactHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell ?: createdCellNewly;
更新
然后检查您的numberOfSectionsInTableView:(UITableView *)tableView
方法返回正确的计数。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// returning count is correct?
}
检查并设置tableview的委托&数据源。