首页 文章

滑动以删除单元格会导致tableViewHeader与单元格一起移动

提问于
浏览
53

我在iOS 8中的 UITableView 上遇到了一个奇怪的错误 tableViewHeader . 当在单元格上滑动以显示删除按钮(标准iOS滑动到删除)时,它会移动 tableViewHeader 以及正在滑动的单元格 . 当我轻扫单元格时, Headers 的移动方式与刷过单元格的方式相同 . 表视图中没有其他单元格被移动,只有 Headers 和任何单元格被刷过 . 我在iOS 7上测试过这个没有遇到过这个问题 . 对我来说,这似乎是iOS 8中 tableViewHeader 的一个错误,因为它只出现在这个版本中,看起来像是永远不会发生的事情 . 我认为没有理由将 Headers 包含在swipe-to-delete中 .

下面只是一个模型 . 在应用程序中滑动删除是默认的iOS,没有自定义 .

Below is just a mockup. Swipe-to-delete within the app is default iOS, nothing custom.

7 回答

  • 0

    基于ferris的答案,我发现使用UITableViewCell作为节头时最简单的方法是返回viewForHeaderInSection中单元格的contentView . 代码如下:

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let  cell : cellSectionHeader = tableView.dequeueReusableCellWithIdentifier("SectionHeader") as cellSectionHeader
        return cell.contentView
        //cellSectionHeader is my subclassed UITableViewCell
    }
    
  • 9

    这是因为我使用UITableViewCell作为表的 Headers . 要解决滑动问题,而不是使用 tableView.tableHeaderView = cell ,我使用以下内容:

    UIView *cellView = [[UIView alloc] init];
    [cellView addSubview:cell];
    tableView.tableHeaderView = cellView
    

    我不知道为什么这会解决问题,特别是它在iOS 7上运行,但它似乎解决了这个问题 .

    确保将所有视图添加到单元格视图,如单元格contentView所示,否则按钮将不响应 .

    作品:

    [cell addSubview:view];[self addSubview:view];

    不起作用:

    [cell.contentView addSubview:view][self.contentView addSubview:view]

  • 0

    避免 Headers 随单元格移动的方法是返回viewForHeaderInSection中单元格的contentView . 如果你有一个名为SectionHeaderTableViewCell的子类UITableViewCell,这是正确的代码:

    -(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        SectionHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SectionHeader"];
        //Do stuff to configure your cell
        return cell.contentView; 
    }
    
  • 25

    SWIFT 3.0 经过测试的解决方案 . 如Objective-C的第一个例子中所述;关键点是返回 cell.contentView 而不是 cell 所以新的格式语法如下 .

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
        // HeaderCell is the name of custom row designed in Storyboard->tableview->cell prototype
        let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
    
        cell.songLabel.text = "Your Section Name"
        return cell.contentView
    }
    
  • 49

    尝试实现此方法并为检查滑动提供适当的条件 . 如果调用此方法进行 Headers 视图 .

    1.tableView:editingStyleForRowAtIndexPath:2.tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:3.tableView:shouldIndentWhileEditingRowAtIndexPath:

  • 3

    .contentView方法未提及的一个问题是,如果表视图单元格使用layoutSubviews,则可能无法获得所需的行为 - 因为不会调用layoutSubviews . 我最终创建了一个完全独立的UIView子类,它支持正常的单元操作和头操作,并创建一个使用它的最小UITableView单元类 .

  • -1

    维克多失去背景颜色的问题,通过以下对布拉德答案的补充解决:

    cell.backgroundColor = UIColor.cyanColor()
    

    至:

    cell.contentView.backgroundColor = UIColor.cyanColor()
    

相关问题