前提・実現したいこと
現在こちらのサイトを参考にTableViewを勉強中です。
必須メソッド宣言時にどうしてもエラーが出現してしまうため、お力添えいただけましたら幸いです。
https://yuu.1000quu.com/use_a_custom_cell_in_swift
発生している問題・エラーメッセージ
class ViewController: UIViewController , UITabBarDelegate, UITableViewDelegate, UITableViewDataSource { 上記のクラス宣言時にエラーが出現します。 Type 'ViewController' does not conform to protocol 'UITableViewDataSource' ______________________________________________________________________ func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tweetIcon.count } 上記を宣言した際に下記のエラーが出現します。 Method 'tableView(tableView:numberOfRowsInSection:)' has different argument names from those required by protocol 'UITableViewDataSource' ('tableView(_:numberOfRowsInSection:)') 3つ目のエラー文は下記となります。 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("tweetCell") as! CustomTableViewCell cell.tweetImageView.image = UIImage(named: tweetIcon[indexPath.row]) cell.tweetNameLabel.text = tweetName[indexPath.row] cell.tweetDescriptionLabel.text = tweetDescriptions[indexPath.row] return cell } __________________________________________________________________________ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 上記を宣言した際に下記の警告文が出現します。 Instance method 'tableView(tableView:cellForRowAtIndexPath:)' nearly matches optional requirement 'tableView(_:heightForRowAt:)' of protocol 'UITableViewDelegate' __________________________________________________________________________ let cell = tableView.dequeueReusableCellWithIdentifier("tweetCell") as! CustomTableViewCell 上記を宣言した際に下記のエラー文が出現します。 'dequeueReusableCellWithIdentifier' has been renamed to 'dequeueReusableCell(withIdentifier:)'
ViewControllerのソースコード
import UIKit class ViewController: UIViewController , UITabBarDelegate, UITableViewDelegate, UITableViewDataSource { let tweetIcon = ["kari", "kari2", "kari", "kari2"] let tweetName = ["test", "テスト", "test2", "テスト2"] let tweetDescriptions = [ "testのツイート内容が記載されます。", "test2のツイート内容が記載されます。", "testの2回目のツイート内容が記載されます。", "test2の2回目のツイート内容が記載されます。" ] override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tweetIcon.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("tweetCell") as! CustomTableViewCell cell.tweetImageView.image = UIImage(named: tweetIcon[indexPath.row]) cell.tweetNameLabel.text = tweetName[indexPath.row] cell.tweetDescriptionLabel.text = tweetDescriptions[indexPath.row] return cell } }
CustomTableViewCellのソースコード
import UIKit class CustomTableViewCell: UITableViewCell { @IBOutlet weak var tweetImageView: UIImageView! @IBOutlet weak var tweetNameLabel: UILabel! @IBOutlet weak var tweetDescriptionLabel: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }
試したこと
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tweetIcon.count }
で出現しているエラーは下記のように_を入れることによって消失しましたが、一方のfuncに対して_を入れてましたが変化は見られませんでした。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tweetIcon.count }
補足情報(FW/ツールのバージョンなど)
xcode9.2
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/03 09:20