如果我插入或删除行,UITableView部分标题将消失

the*_*ncs 26 objective-c uitableview ios

我有UITableView4个部分.其中三个部分有标题视图.

标题视图是a 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这里是我删除重要部分中的行的地方:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

任何帮助,非常感谢.

lou*_*smr 23

只需将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;
}
Run Code Online (Sandbox Code Playgroud)

}


Fra*_*eau 13

添加评论,因为我有同样的问题...问题是我也使用UITableViewCell作为标头而不是使用UITableViewHeaderFooterView.

解决问题:

  1. 创建一个nib文件并在那里创建自定义标题,而不是在故事板上的UITableView中.
  2. 将其链接到您的自定义UITableViewHeaderFooterView类
  3. 在viewDidLoad函数中注册此nib

    override func viewDidLoad() {
        self.tableView.rowHeight = UITableViewAutomaticDimension
    
        self.tableView.register(UINib(nibName: "NibFileName", bundle: nil), forHeaderFooterViewReuseIdentifier: "CustomViewHeaderView")
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 从viewForHeaderInSection函数中取消可重用的HeaderFooterView:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier:"CustomViewHeaderView") as! CustomViewHeaderView
    
        return cell
    }
    
    Run Code Online (Sandbox Code Playgroud)

这修复了我消失的标题.我希望它可以帮助别人.

  • 这绝对感觉像是更干净的解决方案,使用预期的类而不是将单元格包装到视图中。 (2认同)

小智 8

返回contentViewcell很好的工作对我来说。无需将单元格包装在中UIView

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
    return cell.contentView
}
Run Code Online (Sandbox Code Playgroud)


Son*_* VR 5

对于 Swift 4,5

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
}
Run Code Online (Sandbox Code Playgroud)


Vij*_*com -1

我不知道你为什么使用表视图单元格作为标题,但我的假设是

你错过了break语句

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

返回单元格之前检查nil条件,如果为零则创建单元格。

            static NSString *CellIdentifier = @"EditContactHeaderCell";
            EditContactHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

            return cell ?: createdCellNewly;
Run Code Online (Sandbox Code Playgroud)

更新:

然后检查您的numberOfSectionsInTableView:(UITableView *)tableView方法返回正确的计数。

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
         // returning count is correct?
    }
Run Code Online (Sandbox Code Playgroud)

检查并设置tableview的委托和数据源。