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

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

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

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

Swift

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

Q&A

解決済

1回答

1165閲覧

tableViewControllerの返却処理のcountにエラー

aulthy787

総合スコア8

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

Swift

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

0グッド

0クリップ

投稿2019/11/09 12:20

Swiftで設定画面を作っています。
設定画面をtableViewController(A)で作成し、そのうちのcell(1)を新たなtableViewController(B)にshowでつなげました。
tableViewController(B)に対してcocoafileを作成し、コードを書きました。
すると、配列の長さを返却するコードにエラーが発生しました。
ご指摘していただける幸いです。
よろしくお願い致します。

発生している問題・エラーメッセージ

Value of type 'Int' has no member 'count'

該当のソースコード

swift

1return section.count

試したこと

初めはtableViewController(A)のcocoafileに書いていましたが、tableViewController(A)のcellを表示する** override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int**にエラーが発生したため、fileを分割したところそちらのエラーが消えて新たにcountにエラーが発生しました。
サンプルのコードをコピペしても改善されないためコードに誤りはないと思います。

補足情報(FW/ツールのバージョンなど)

xcode11.1を使用しています。
入力したコードをこちらに貼っておきます。

import UIKit

class CategoryTableViewController: UITableViewController {

override func viewDidLoad() { super.viewDidLoad() // 保存しているToDoの読み込み処理 let userDefaults = UserDefaults.standard if let storedTodoList = userDefaults.object(forKey: "section") as? Data { do { if let unarchiveTodoList = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSArray.self, Cathouse.self], from: storedTodoList) as? [Cathouse] { section.append(contentsOf: unarchiveTodoList) } } catch { // エラー処理なし } } } var section = [Cathouse]() @IBAction func addCategory(_ sender: Any) { let alertController = UIAlertController(title: "カテゴリー追加", message: "カテゴリーを追加してください", preferredStyle: UIAlertController.Style.alert) alertController.addTextField(configurationHandler: nil) let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default) {(action: UIAlertAction) in if let textField = alertController.textFields?.first { let catHouse = Cathouse() catHouse.catTitle = textField.text! self.section.insert(catHouse, at: 0) self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: UITableView.RowAnimation.right) // ToDoの保存処理 let userDefaults = UserDefaults.standard // Data型にシリアライズする do { let data = try NSKeyedArchiver.archivedData(withRootObject: self.section, requiringSecureCoding: true) userDefaults.set(data, forKey: "todoList") userDefaults.synchronize() } catch { // エラー処理なし } } } alertController.addAction(okAction) let cancelButton = UIAlertAction(title: "CANCEL", style: UIAlertAction.Style.cancel, handler: nil) alertController.addAction(cancelButton) present(alertController, animated: true, completion: nil) } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // Todoの配列の長さを返却する ###return section.count } // テーブルの行ごとのセルを返却する override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // Storyboardで指定したtodoCell識別子を利用して再利用可能なセルを取得する let cell = tableView.dequeueReusableCell(withIdentifier: "catecell", for: indexPath) // 行番号に合ったToDoの情報を取得 let catHouse = section[indexPath.row] // セルのラベルにToDoのタイトルをセット cell.textLabel?.text = catHouse.catTitle } // セルをタップしたときの処理 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let catHouse = section[indexPath.row] if catHouse.catDone { // 完了済みの場合は未完了に変更 catHouse.catDone = false } else { // 未完の場合は完了済みに変更 catHouse.catDone = true } // セルの状態を変更 tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.fade) // データ保存。Data型にシリアライズする do { let data: Data = try NSKeyedArchiver.archivedData(withRootObject: section as Array, requiringSecureCoding: true) // UserDefaultsに保存 let userDefaults = UserDefaults.standard userDefaults.set(data, forKey: "section") userDefaults.synchronize() } catch { } } // セルを削除したときの処理 override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { // 削除処理かどうか if editingStyle == UITableViewCell.EditingStyle.delete { // ToDoリストから削除 section.remove(at: indexPath.row) // セルを削除 tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.fade) // データ保存。Data型にシリアライズする do { let data: Data = try NSKeyedArchiver.archivedData(withRootObject: section, requiringSecureCoding: true) // UserDefaultsに保存 let userDefaults = UserDefaults.standard userDefaults.set(data, forKey: "catList") userDefaults.synchronize() } catch { // エラー処理なし } } } } // 独自クラスをシリアライズする際には、NSObjectを継承し // NSSecureCodingプロトコルに準拠する必要がある class Cathouse: NSObject, NSSecureCoding { static var supportsSecureCoding: Bool { return true } // ToDoのタイトル var catTitle: String? // ToDoを完了したかどうかを表すフラグ var catDone: Bool = false // コンストラクタ override init() { } // NSCodingプロトコルに宣言されているデシリアライズ処理。デコード処理とも呼ばれる required init?(coder aDecoder: NSCoder) { catTitle = aDecoder.decodeObject(forKey: "catTitle") as? String catDone = aDecoder.decodeBool(forKey: "catDone") } // NSCodingプロトコルに宣言されているシリアライズ処理。エンコード処理とも呼ばれる func encode(with aCoder: NSCoder) { aCoder.encode(catTitle, forKey: "catTitle") aCoder.encode(catDone, forKey: "catDone") } }

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

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

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

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

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

guest

回答1

0

ベストアンサー

おそらくですがメンバ変数の sectionfunc tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int の引数の section で名前が被っており、引数の section: Int が呼ばれているのが原因だと思います。
下記のようにselfをつければ解決すると思います。

swift

1return self.section.count

コード部分はMarkdown記法 チートシートのようにバッククオートで囲った方がみやすいので回答がつきやすいと思います。

投稿2019/11/09 15:37

usagi001

総合スコア208

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

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

aulthy787

2019/11/09 16:12

ご回答ありがとうございます! sectionを改善したところ、エラーは改善され実行することができました。 また、コードの書き方についてもURL添付でご指導していただきありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問