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

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

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

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

Swift

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

Q&A

0回答

415閲覧

Type of expression is ambiguous without more contextとValue of type 'Player' has no subscriptsというエラー

misokota

総合スコア36

Xcode

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

Swift

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

0グッド

0クリップ

投稿2021/08/19 05:41

import UIKit class Player { let name: String let image: UIImage init(name: String, image: UIImage) { self.name = name self.image = image } } class MainTableViewController: UITableViewController { private let cellId = "cellId" private let players = [ Player(name: "竹富", image: #imageLiteral(resourceName: "IMG_6427")) ] override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white tableView.register(MainTableViewCell.self, forCellReuseIdentifier: cellId) setupNavigationBar() } private func setupNavigationBar() { navigationItem.title = "NBA Player" navigationItem.largeTitleDisplayMode = .always navigationController?.navigationBar.prefersLargeTitles = true //navigationの背景色を変更 let appearance = UINavigationBarAppearance() appearance.configureWithDefaultBackground() appearance.backgroundColor = UIColor(white: 0.9, alpha: 1) navigationController?.navigationBar.scrollEdgeAppearance = appearance navigationController?.navigationBar.standardAppearance = appearance } } //MARK - tableViewの設定 extension MainTableViewController { override func numberOfSections(in tableView: UITableView) -> Int { return players.count } override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 50 } override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let label = UILabel() label.text = "Aチーム" label.textAlignment = .center label.backgroundColor = .darkGray label.textColor = .white switch section { case 0: label.text = "Aチーム" case 1: label.text = "Bチーム" case 2: label.text = "Cチーム" default: break } return label } override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 60 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return players[section].count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! MainTableViewCell cell.nameLabel.text = players[indexPath.section][indexPath.row] //cell.textLabel?.text = players[indexPath.section][indexPath.row] return cell } }

Xcode初心者です。
上のコードをうつと、
下から約11行にある return players[section].countにType of expression is ambiguous without more contextというエラーがでて、
その下のcell.nameLabel.text = players[indexPath.section][indexPath.row]にValue of type 'Player' has no subscriptsというエラーが出てきます。
解決方法を教えて欲しいです。

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

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

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

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

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

hoshi-takanori

2021/08/19 11:21

players[section].count や players[indexPath.section][indexPath.row] をするためには players が 2 次元配列 (配列の配列) になってる必要がありますが、実際には単なる Player の配列だからでは。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問