Как показать ячейку UITableView в UITableView без массива? - PullRequest
0 голосов
/ 11 мая 2018

У меня есть этот код:

    class UserProfilViewController: UIViewController {
    // Outlets
    @IBOutlet weak var userProfileTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.navigationItem.title = "Profil"
    }

}

// MARK: - Table View Data Source
extension UserProfilViewController {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UserProfilCell", for: indexPath)
        return cell
    }



}

Мой проект в bitbucket: https://bitbucket.org/trifek/karta-nauka/src/master/

Я поместил одну ячейку tableviewcell в tableview (UserProfil.storyboard).У меня есть форма, которую я хотел бы отобразить в этой ячейке.Проблема в том, что ячейка не отображается.Кто-нибудь знает как это исправить?

Ответы [ 4 ]

0 голосов
/ 15 мая 2018

Вы никогда не заявляете, что ваш контроллер представления соответствует протоколам UITableViewDataSource или UITableViewDelegate.Учитывая, что вы этого не делаете, вы не сможете установить свой контроллер представления в качестве источника данных или делегата вашего табличного представления.

0 голосов
/ 11 мая 2018

В соответствии с кодом, которым вы поделились, измените ваш код на следующий.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "UserProfilCell", for: indexPath) as! UserProfilTableViewCell
    return cell
}

Дайте мне знать, если у вас возникнут вопросы.

0 голосов
/ 14 мая 2018
    @Lukasz 

    Please use the below code for this.

    class UserProfileViewController: UIViewController {

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
               setUIComponents()
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        private func setUIComponents(){

            registerNibs()
        }
    }

    extension UserProfileViewController: UITableViewDelegate, UITableViewDataSource{

    internal func registerNibs(){

            tableView.register(UINib(nibName: String(describing:  UserProfileTableCell.self), bundle: Bundle.main), forCellReuseIdentifier:     kUserProfileCellReuseId)
        }

        //MARK: TableView Methods -
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return 5
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let sessionCell: UserProfileTableCell.self = tableView.dequeueReusableCell(withIdentifier: kUserProfileCellReuseId, for: indexPath) as! UserProfileTableCell.self
            cell.titleLabel.text = “TEST”
            return sessionCell
        }

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath, animated: true)
        }
    }

class UserProfileTableCell: UITableViewCell {

//Set the "kUserProfileCellReuseId" in nib to register identifier.
let kUserProfileCellReuseId = "kUserProfileCellReuseId"

//MARK: - Override Methods
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        setUIComponents()
    }

    private func setUIComponents(){
    }
}
0 голосов
/ 11 мая 2018

ИМХО, сначала попробуйте очистить ваши требования.Если вы хотите отобразить фиксированное количество ячеек , тогда вы можете просто использовать статические ячейки .Если ваши ячейки динамические , то есть их количество зависит от некоторых вычислений или другой логики, тогда вы можете использовать динамические ячейки .При использовании динамической ячейки проверьте, зарегистрировали ли вы ее или нет (если вы используете xib для ячейки), а также проверьте ячейку identifier.

...