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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

2回答

2250閲覧

UITableViewでテーブル表示させようとすると、エラーばかりでます。ネットで調べてもわからないのですが、どこが悪いのでしょうか?

ShoheiOkamoto

総合スコア11

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2015/12/17 13:38

編集2015/12/17 14:17

※エラー部分にコメントアウトを入れています

import UIKit

class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{

private let myItems : NSArray = ["TEST1","TEST2","TEST3"] private var myTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let barHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.size.height let displayWidth: CGFloat = self.view.frame.width let displayHeight: CGFloat = self.view.frame.height myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight)) myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell") myTableView.dataSource = self myTableView.delegate = self self.view.addSubview(myTableView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print("Num: \(indexPath.row)") print("Value: \(myItems[indexPath.row])") } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return myItems.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

//ここより下からエラーが出ます
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath)
//エラー内容→ Constant'cell' inferred to have type 'AnyObject',which may be unexpected

cell.textLabel!.text = "(myItems[indexPath.row])"//エラー内容→ 'UILabel?' does not have a member named 'text'

return cell//エラー内容→ Type'AnyObject' cannot be implicity downcast to 'UITableViewCell';did you mean to use 'as' to force downcast?
}

}

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

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

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

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

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

sekitaka_1214

2015/12/17 14:08

エラーの内容も記載してもらえますか?
ShoheiOkamoto

2015/12/17 14:17

エラー内容を追記しました!よろしくお願いいたします。
guest

回答2

0

たしかにxcode7.2で正常に動きますね。

投稿2015/12/17 15:38

domonr

総合スコア263

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

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

ShoheiOkamoto

2015/12/21 15:08

ありがとうございます!アップデートしたら、動きました!
guest

0

ベストアンサー

新規のプロジェクトのViewControllerにソースを貼り付けてエラー無くテーブルが表示できました。
※Storyboardの変更無し

こちらの環境はXcode7.2 Swift2.1.1 です。

swiftのバージョンが古い場合キャストが必要かもしれません。

swift

1var cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as UITableViewCell 2 3or 4 5var cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! UITableViewCell

投稿2015/12/17 14:29

編集2015/12/17 14:57
_Kentarou

総合スコア8490

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

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

ShoheiOkamoto

2015/12/21 15:09

ありがとうございます!先ほどXcodeをアップデートしたら、動きました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問