質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2738閲覧

TableViewの必須メソッド宣言時のエラーについて

ottotto

総合スコア22

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/11/03 08:26

編集2018/11/03 08:29

前提・実現したいこと

現在こちらのサイトを参考に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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

答えることはできるんですが、多分解決しないので・・・

そのサイトはこう書いています
>今回解説を行う環境です。
>OS:OS X El Capitan10.11.4
>Xcode:7.3
>Swift:2.2

Swiftのバージョンは上がるごとに書式(正しい書き方)がかなり変わります
もし今学習するなら、Swift4系(4.0,4.1,4.2)か、最低でもSwift3系で探してみてください
Swift4系と2.2系の差の話をするのは結構厳しいです

投稿2018/11/03 08:49

kosanai

総合スコア471

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ottotto

2018/11/03 09:20

ご回答ありがとうございます。 swiftのバージョンのよって多少文法が異なることは小耳に挟んでおりましたが、そこまで仕様が変わるケースもあるんですね。 勉強になります。 swift4で改めて調べたところ、下記のように書き換えることによってエラーコードが消失いたしました。 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "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 } エラーコードはなく、ビルドも問題なくできるのですが、アプリを開いたところ一瞬で暗転してアプリが強制終了してしまいます。 その際にエラーコードなども表示されておりませんが、デバッグなどで原因を追求したいと思っております。 またわからない点があった場合は、別途質問させていただくかもしれません。 この度は素早い回答誠にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問