UITableView источник данных - PullRequest
0 голосов
/ 24 марта 2020

Я получаю 2 ошибки.

Первое, что меня смущает, потому что все находится внутри {}, поэтому я не уверен, где я ошибаюсь.

"Тип" StoriesViewController "не соответствует протоколу Исправление щелчка «UITableViewDataSource» добавляет:

прикол c tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {<# code #>}

и 2-я ошибка

Неоднозначное использование 'dequeueReusableCellWithIdentifier

Я присвоил ячейке таблицы идентификатор "storyCell" из основной раскадровки

Полный код:

// creating a custom color
extension UIColor {
    static var darkmodeGray = UIColor.init(red: 41/255, green: 42/255, blue: 47/255, alpha: 1)
}


class StoriesViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        <#code#>
    }


    @IBOutlet weak var tableView: UITableView!

    let storySelection = [("WeekOne"),("WeekTwo"),("WeekThree"),("WeekFour"),("WeekFive"),("WeekSix")]
    let storySelectionImages = [UIImage(named: "WeekOne"), UIImage(named: "WeekTwo"), UIImage(named: "WeekThree"), UIImage(named: "WeekFour"), UIImage(named: "WeekFive"), UIImage(named: "WeekSix")]



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

        self.view.backgroundColor = UIColor .darkmodeGray
    }

    //Part One
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    //Part Two
    func  tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return storySelection.count
    }

    //Part Three
    private func tableView(tableView :UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell=tableView.dequeueReusableCellWithIdentifier("storyCell", forIndexPath: IndexPath) as! TableViewCell
        cell.CoverArt.image=self.storySelectionImages[indexPath .row]
        return cell
    }
}

1 Ответ

2 голосов
/ 24 марта 2020

Ваш код - это код Swift 2 (!).

Обычный способ обновить сигнатуру метода - это закомментировать, повторить его и использовать завершение кода.

Или прочитать документация , это всегда хорошая идея.

Три метода: (numberOfSections можно опустить, если есть только один раздел)

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "storyCell", for: indexPath) as! TableViewCell
    cell.CoverArt.image = self.storySelectionImages[indexPath.row]
    return cell
}
...