Почему я не могу добавить свой пользовательский объект в массив? - PullRequest
2 голосов
/ 26 января 2020

поэтому сначала я создал пользовательский объект:

import Foundation
class Website { 
             var name:String
             var pictureLabel:String

    init(title:String,pictureLabel:String) {
        name = title
        pictureLabel = picture
    }
}

, а затем в моем классе tableviewcontroller:

class ViewController:
 UIViewController,UITableViewDelegate,UITableViewDataSource {

    var websites = [Website]()

    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 80
        tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell")


        websites.append(Website(title: "facebook.com", pictureLabel: "facelogo"))


        print(websites)
    }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return websites.count
       }

       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
        cell.label.text = websites[indexPath.row].name
        return cell }

ошибка: фатальная ошибка: неожиданно обнаружен ноль при неявном развертывании Необязательное значение: file

1 Ответ

0 голосов
/ 26 января 2020

проблема была в этой строке

tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell")

это должно быть

 tableView.register(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "MyCell")`
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...