如何修复 didSelectRowAt 返回的错误 indexPath?
Posted
技术标签:
【中文标题】如何修复 didSelectRowAt 返回的错误 indexPath?【英文标题】:How to fix wrong indexPath returned by didSelectRowAt? 【发布时间】:2019-11-08 03:56:18 【问题描述】:我有一个 UITableView;没有 tableHeaderView 点击一行并触发 didSelectRowAt 返回正确的索引路径。
当我设置 tableHeaderView 属性时,didSelectRowAt 要么不触发,要么返回 tappedRow + 2。我哪里出错了?
这是我的代码
class MenuController: UIViewController
// Mark -- Properties
var tableView: UITableView!
var delegate: HomeControllerDelegate?
var headerView: HeaderView? = nil
var user: User? = nil
// Mark -- Init
override func viewDidLoad()
super.viewDidLoad()
configureTableView()
if let user = self.user
populateMenuHeader(email: user.email, firstName: user.firstName, lastName: user.lastName, imageUrl: user.imageUrl)
override func viewWillLayoutSubviews()
super.viewWillLayoutSubviews()
//updateHeaderViewHeight(for: tableView.tableHeaderView)
func updateHeaderViewHeight(for header: UIView?)
guard let headerView = headerView else return
headerView.frame.size.height = 170
func populateMenuHeader(email: String, firstName: String, lastName: String, imageUrl: String)
headerView?.emailLabel?.text = email
headerView?.nameLabel?.text = "\(firstName) \(lastName)"
let request = ImageRequest(
url: URL(string: imageUrl)!,
processors: [
ImageProcessor.Resize(size: CGSize(width: 70, height: 70)),
ImageProcessor.Circle()
]
)
Nuke.loadImage(with: request, into: headerView!.imageView!)
// Mark -- Handlers
func configureTableView()
// Create Material Header
headerView = HeaderView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 170))
//headerView?.heightAnchor.constraint(equalToConstant: 170).isActive = true
headerView?.translatesAutoresizingMaskIntoConstraints = false
tableView = UITableView()
tableView.delegate = self
tableView.dataSource = self
tableView.tableHeaderView = headerView
tableView.sectionHeaderHeight = 170
tableView.register(MenuOptionCell.self, forCellReuseIdentifier: reuseIdentifier)
tableView.backgroundColor = .darkGray
tableView.separatorStyle = .none
tableView.rowHeight = 80
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
extension MenuController: UITableViewDelegate, UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return 5
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! MenuOptionCell
let menuOption = MenuOption(rawValue: indexPath.row)
cell.descriptionLabel.text = menuOption?.description
cell.iconImageView.image = menuOption?.image
return cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
let row = indexPath.row
print("tapped row: \(row)")
let menuOption = MenuOption(rawValue: row)
delegate?.handleMenuToggle(forMenuOption: menuOption)
class CustomView: UIView
override func draw(_ rect: CGRect)
super.draw(rect)
if let context = UIGraphicsGetCurrentContext()
context.setStrokeColor(UIColor.white.cgColor)
context.setLineWidth(1)
context.move(to: CGPoint(x: 0, y: bounds.height))
context.addLine(to: CGPoint(x: bounds.width, y: bounds.height))
context.strokePath()
class HeaderView : UIView
var imageView: UIImageView? = nil
var nameLabel: UILabel? = nil
var emailLabel: UILabel? = nil
override init(frame: CGRect)
super.init(frame: frame)
imageView = UIImageView()
imageView?.translatesAutoresizingMaskIntoConstraints = false
nameLabel = UILabel()
nameLabel?.translatesAutoresizingMaskIntoConstraints = false
nameLabel?.font = UIFont(name: "Avenir-Light", size: 20)
nameLabel?.text = "Test name"
nameLabel?.textColor = .white
emailLabel = UILabel()
emailLabel?.translatesAutoresizingMaskIntoConstraints = false
emailLabel?.textColor = .white
emailLabel?.font = UIFont(name: "Avenir-Light", size: 15)
emailLabel?.text = "testemail@gmail.com"
self.addSubview(imageView!)
self.addSubview(nameLabel!)
self.addSubview(emailLabel!)
let lineView = CustomView(frame: CGRect(x: 0, y: frame.height - 1, width: frame.width, height: 1))
self.addSubview(lineView)
imageView?.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
imageView?.topAnchor.constraint(equalTo: topAnchor, constant: 20).isActive = true
imageView?.widthAnchor.constraint(equalTo: widthAnchor, constant: 70).isActive = true
imageView?.heightAnchor.constraint(equalTo: heightAnchor, constant: 70).isActive = true
nameLabel?.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
nameLabel?.topAnchor.constraint(equalTo: imageView!.bottomAnchor, constant: 10).isActive = true
emailLabel?.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
emailLabel?.topAnchor.constraint(equalTo: nameLabel!.bottomAnchor, constant: 5).isActive = true
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
【问题讨论】:
【参考方案1】:问题似乎是由于您没有正确设置标题视图的高度。 tableHeaderView
的文档指出:
将视图分配给此属性时,将该视图的高度设置为非零值。表格视图只考虑视图框架矩形的高度;它会自动调整标题视图的宽度以匹配表格视图的宽度。
更新您的标题视图代码:
变化:
headerView = HeaderView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 170))
//headerView?.heightAnchor.constraint(equalToConstant: 170).isActive = true
headerView?.translatesAutoresizingMaskIntoConstraints = false
...
tableView.sectionHeaderHeight = 170
只是:
headerView = HeaderView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 170))
就是这样。无需弄乱约束。无需设置无关的部分高度。只需为标题视图的框架指定所需的高度即可。
【讨论】:
谢谢!禁用自动布局修复行点击。但现在我的 HeaderView 显示非常不稳定。为什么启用自动布局可以解决行敲击问题?我希望 tableView 能够正确地将点击与 indexPath 匹配,并且仅使用高度为 170 的 CGRect 初始化 headerView。 您的评论自相矛盾。首先你说禁用自动布局可以修复行点击,然后你问为什么启用它可以解决问题。 抱歉!为什么启用***自动布局可以解决行敲击问题? 但未启用自动布局。解决方法是(正确地)依赖提供的框架。当您执行translatesAutoresizingMaskIntoConstraints = false
时,您是在告诉自动布局忽略视图的框架并依赖约束。
知道了。我误解了一些基本的东西。要去打文档。感谢您的指导!以上是关于如何修复 didSelectRowAt 返回的错误 indexPath?的主要内容,如果未能解决你的问题,请参考以下文章
带有 didselectrowAt 索引的 UiTableview 排序返回先前的选择?
当通过点击背景关闭 UITextView 的键盘时,我在 didSelectRowAt 中得到错误的 indexPath