swiftでテーブルビューを作ろうとしています。
MainStoryboradには何も載せていません。
下記のコードだけを打ち込んで作成しています。
シュミレーターを開いてみると画面が真っ白なままでテーブルビューが表示されません。
まだまだ勉強不足なので合っているかわかりませんが、恐らくコンソールという場所に
「 Unknown class _TtC25 ファイル名 ViewController in Interface Builder file.」というのが表示されています。
アドバイスをお願いします。
import UIKit let sectionTitle=["蝶目","バッタ目","甲虫目"] let section0=[("キタテハ","タテハチョウ科"),("クロアゲハ","アゲハチョウ科")] let section1=[("キリギリス","キリギリス科"),("ヒナバッタ","バッタ科"),("マツムシ","マツムシ科")] let section2=[("ハンミョウ","ハンミョウ科"),("アオオサムシ","オサムシ科"),("チビクワガタ","クワガタムシ科")] let titleData = [section0,section1,section2] class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let myTableView:UITableView! myTableView = UITableView(frame: view.frame, style: .grouped) myTableView.delegate = self myTableView.dataSource = self view.addSubview(myTableView) } func numberOfSections(in tableView: UITableView) -> Int { return sectionTitle.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let sectionData = titleData[section] return sectionData.count } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return sectionTitle[section] } func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath)->UITableViewCell{ let cell = UITableViewCell(style: .subtitle,reuseIdentifier:"cell") let sectionData = titleData[(indexPath as NSIndexPath).section] let cellData = sectionData[(indexPath as NSIndexPath).row] cell.textLabel?.text = cellData.0 cell.detailTextLabel?.text = cellData.1 return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let title = sectionTitle[indexPath.section] let sectionData = titleData[indexPath.section] let cellData = sectionData[indexPath.row] print("(title)(cellData.1)") print("(cellData.0)") } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/03 17:54
2018/11/03 23:31
2018/11/04 05:24