Как исправить неправильный indexPath, возвращаемый didSelectRowAt? - PullRequest
0 голосов
/ 08 ноября 2019

У меня есть 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")
    }
}

Ответы [ 2 ]

1 голос
/ 08 ноября 2019

Проблема, по-видимому, связана с тем, что вы неправильно настраиваете высоту представления заголовка. Документация для 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))

Вот и все. Не нужно связываться с ограничениями. Нет необходимости устанавливать несвязанную высоту сечения. Просто дайте рамке вида заголовка нужную высоту.

0 голосов
/ 08 ноября 2019

UITableView имеет методы delegate для добавления заголовка, т. Е.

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

public func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {}

public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {}

Все вышеперечисленные методы полезны, когда вы хотите добавить headerView к вашему UITableView и управлять ихвысоты в зависимости от ваших ограничений, например, когда ваш User объект nil, тогда вы не хотите показывать или что-то в этом роде.

Итак, я изменил ваш код и могу успешно обнаружить нажатие UITableView.

public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = HeaderView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 170))
    headerView.translatesAutoresizingMaskIntoConstraints = false
    headerView.emailLabel?.text = "email"
    headerView.nameLabel?.text = "firstName lastName"
    return headerView
}

public func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
    return 170
}

public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 170
}

Так вы управляете видом заголовка. Дайте мне знать, если это работает или нет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...