【问题标题】:swift block selection of custom section header cell自定义部分标题单元格的快速块选择
【发布时间】:2018-10-09 10:52:33
【问题描述】:

我有一个带有自定义节标题的 UITableView,它是通过故事板使用带有“headerCell”标识符的自定义原型单元格以及一个名为“HeaderViewCell”的子类 UITableViewCell 的 Cocoa Touch 类制作的。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerCell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! HeaderViewCell

    headerCell.sectionTitle.text = viewModel.items[section].sectionTitle
    headerCell.section = section
    headerCell.delegate = self

    return headerCell
}

单元格中的按钮会触发一个委托函数,该函数会传入分配给它的部分。

一切都很好 - 设置标题,点击我需要的按钮等......除了当您点击部分标题(左侧)和按钮(右侧)之间的空白区域时,部分标题会突出显示就好像它是节中的一个单元格一样,然后对节中的第一行执行segue。

在属性检查器中选择设置为“无”。如果我切换启用用户交互,则该按钮不起作用。

我发现了很多帖子,其中人们试图在部分标题上注册点击(答案:使用点击手势),但我已经筋疲力尽地寻找如何阻止它们。在委托方法的 didSelectRow 中,我看到了与单击行而不是标题相同的 IndexPath,因此我无法从那里阻止它。

由于使用自定义原型单元格是对自定义节标题的最广泛建议的响应,我本以为这对其他人来说也是一个问题。 ?

【问题讨论】:

  • 你想阻止 headerCell 突出显示还是阻止它执行 segue?
  • Lance,节标题既突出显示又执行 segue-节标题不应该做任何这些事情。点击标题会导致 didSelectRow print("section: (indexPath.section) | row: (indexPath.row)") section: 1 | row: 0 没有区分节标题和第一行 [0]
  • 马特的回答应该有效。
  • 马特的回答不起作用。它停止选择和继续的唯一方法是设置为不进行用户交互。

标签: ios swift uitableview


【解决方案1】:

马特的回答应该会奏效。

创建一个UITableViewHeaderFooterView 类型的子类并将其命名为CustomHeaderView

class CustomHeaderView: UITableViewHeaderFooterView {
   // programmatically add the sectionTitle and whatever else inside here. Matt said there isn’t a storyboard or nib for a HeaderFooterView so do it programmatically
}

然后在 viewForHeaderInSection 内部使用 tableView.dequeueReusableHeaderFooterView 并将其转换为 CustomHeaderView

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    // don't forget to rename the identifier 
    let customHeaderView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "customHeaderView") as! CustomHeaderView

    customHeaderView.sectionTitle.text = viewModel.items[section].sectionTitle
    customHeaderView.section = section
    customHeaderView.delegate = self

    return customHeaderView
}

如果不试试这个。

如果您不希望单元格突出显示,请先将选择样式设置为.none

在 HeaderCell 中设置 .selectionStyle = .none

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerCell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! HeaderViewCell

    headerCell.sectionTitle.text = viewModel.items[section].sectionTitle
    headerCell.section = section
    headerCell.delegate = self
    headerCell.selectionStyle = .none // set it here

    return headerCell
}

然后在didSelectRowAtIndexPath 中找出被选中的单元格类型。如果是HeaderCell,那么只需return,并且单元格不应该推动。如果它是任何其他类型的单元格(例如 PushCell),那么这些单元格应该执行 segue:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    // if it's a HeaderCell then do nothing but print
    if let _ = tableView.cellForRowAtIndexPath(indexPath) as? HeaderCell {
        tableView.deselectRow(at: indexPath, animated: true)
        print("+++++HeaderCell was tapped")
        return // nothing should happen
    }

    // if it's a PushCell then push
    if let _ = tableView.cellForRowAtIndexPath(indexPath) as? PushCell {
        print("-----PushCell was tapped")
        performSegue(withIdentifier...
        // or if your using navigationController?.pushViewController(...
    }
}

【讨论】:

  • 感谢您花时间考虑这个问题......但是从代码中设置 .selectionStyle 并不会改变行为。如果将其转换为 HeaderCell 也没有捕获,它仍会为该部分返回一个“正常”行单元格,因为索引路径是该部分中第一行的路径。
  • 让我们一次解决 1 个问题。 HeaderCell 是不再推送还是还在推送?
  • 看看我刚刚在 HeaderCell 的 if let 中的 didSelectRowAtIndexPath 中添加了什么 >>>tableView.deselectRow(at: indexPath, animated: true)
【解决方案2】:

“HeaderViewCell”子类化 UITableViewCell。

停在那里。这是完全错误的。您的部分标题不应该是 UITableViewCell。它应该是 UITableViewHeaderFooterView(或其子类)。

一旦您进行了更改(以及对标题视图类型注册的任何所需的相应更改),您的问题就会消失。

【讨论】:

  • 我得到“从 'UITableViewCell 投射?'到不相关的类型 'ListsHeaderViewCell' 总是失败”viewForHeaderInSection 返回一个 UIView
  • 我无法指导您完成您需要进行的所有更改,因为之前做错了。请注意,您不能在情节提要中制作原型标题;如果你认为你之前做的正确,那你就错了
  • 如果您需要一个如何在代码中创建自定义标头的示例,请参阅github.com/mattneub/Programming-iOS-Book-Examples/blob/master/…
  • 使用马特的回答,您需要将 UITableViewCell 更改为 UITableViewHeaderFooterView
  • 再次感谢您的帮助.. 但正如“我并不完全疯了”的注释:这是整个网络广泛接受的做法,请参阅 stackoverflow.com/questions/9219234/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-25
  • 2020-03-19
  • 1970-01-01
  • 2020-11-19
相关资源
最近更新 更多