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

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

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

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

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

Xcode

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

Swift

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

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

2回答

3297閲覧

swiftでのTableViewに関する質問

summer

総合スコア11

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

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

Xcode

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

Swift

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

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

0クリップ

投稿2015/05/12 16:11

swift docsというサイトにある UITableViewに追加・削除機能を追加 という項目で、TableViewやNavigationControllerなどをstoryboardで実装して同じ機能を付けたいのですがうまくいきません。現在のViewController.swiftのコードはこちらです---

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

let myItems: NSMutableArray = ["TEST1", "TEST2", "TEST3"] var myTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() self.navigationItem.rightBarButtonItem = self.editButtonItem() myTableView = UITableView() myTableView.allowsSelectionDuringEditing = true myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return myItems.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell=tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell cell.textLabel?.text="\(myItems[indexPath.row])" return cell } func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { var sec=0 sec=section+1 return "セクション"+sec.description } /* 編集ボタンが押された際に呼び出される */ override func setEditing(editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) // TableViewを編集可能にする myTableView.setEditing(editing, animated: true) // 編集中のときのみaddButtonをナビゲーションバーの左に表示する if editing { println("編集中") let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "addCell:") self.navigationItem.setLeftBarButtonItem(addButton, animated: true) } else { println("通常モード") self.navigationItem.setLeftBarButtonItem(nil, animated: true) } } /* addButtonが押された際呼び出される */ func addCell(sender: AnyObject) { println("追加") // myItemsに追加. myItems.addObject("add Cell") // TableViewを再読み込み. myTableView.reloadData() } /* Cellを挿入または削除しようとした際に呼び出される */ func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { // 削除のとき. if editingStyle == UITableViewCellEditingStyle.Delete { println("削除") // 指定されたセルのオブジェクトをmyItemsから削除する. myItems.removeObjectAtIndex(indexPath.row) // TableViewを再読み込み. myTableView.reloadData() } }

}

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

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

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

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

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

guest

回答2

0

自己解決

できました!!

どうやらConstraintsの設定をしていなかったのが原因らしく、画面の右側がシミュレーターでひょうじされていなかったようです。

お世話になりました!!
また機会がありましたら回答宜しくお願いします。

投稿2015/05/13 13:40

summer

総合スコア11

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

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

退会済みユーザー

退会済みユーザー

2015/05/14 00:19

summerさん、よかったですね。 あまり役に立てませんでしたが、解決できてなによりです。
guest

0

こんにちは。

Cellに値を渡す箇所にある

lang

1let cell=tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

lang

1let cell=tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell // 「!」を削除

と、as演算子のエクスクラメーションマーク(!)を削除すれば、このままでもビルドは(とりあえず)成功します。

ただし、DataSourceや Delegateの設定や、編集中の Cellの選択が許可されていないだけでなく、TableViewが Viewに追加されていませんので、テーブルは表示されないのではないかと思うのですが、いかがでしょうか。
もういちど、参考にされたソースコードをごらんになることをおすすめします。

投稿2015/05/13 01:49

編集2015/05/13 02:56
退会済みユーザー

退会済みユーザー

総合スコア0

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

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

summer

2015/05/13 03:32

回答ありがとうございます。 少し自分の説明不足のようです。元のソースではTableViewなどが全てコードで実装されているのですが、それをTableViewなどの実装をstoryboard上、GUIで実装した上で同じ機能をつけたいと思っているところです。 つまりはセルやTableViewはstoryboard上で実装してある状態です。 開発を始めてまだ間もないため質問の仕方やわけわからない部分がコードにあるかと思いますがすみません。
退会済みユーザー

退会済みユーザー

2015/05/13 04:34

summerさん、コメント(返信)をいただきありがとうございます。 storyboardを使用して TableViewを実装した場合には、 @IBOutlet weak var tableView: UITableView! といったコードが記述されているはずなのですが…… これは、storyboardで追加した TableViewを ViewController.swiftに関連づけることでソースコードとして反映されるものです。 もしも、関連づけの作業を行っているようでしたら、これは無視してください。 そうであれば、最低限必要なメソッドは書かれていますので、ビルドすればなにかしら表示されるはずです。
summer

2015/05/13 05:26

またまたありがとうございます。 アドバイスを元に少し書き直してみました。 import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var TableView: UIView! @IBOutlet weak var myTableView: UITableView! @IBOutlet weak var MyCell: UITableViewCell! let myItems: NSMutableArray = ["TEST1", "TEST2", "TEST3"] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. myTableView.delegate = self myTableView.dataSource = self myTableView.allowsSelectionDuringEditing = true } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* Cellの総数を返す (実装必須) */ func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return myItems.count } /* Cellに値を設定する (実装必須) */ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! UITableViewCell // Cellに値を設定. cell.textLabel?.text = "\(myItems[indexPath.row])" return cell } /* 編集ボタンが押された際に呼び出される */ override func setEditing(editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) // TableViewを編集可能にする myTableView.setEditing(editing, animated: true) // 編集中のときのみaddButtonをナビゲーションバーの左に表示する if editing { println("編集中") let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "addCell:") self.navigationItem.setLeftBarButtonItem(addButton, animated: true) } else { println("通常モード") self.navigationItem.setLeftBarButtonItem(nil, animated: true) } } /* addButtonが押された際呼び出される */ func addCell(sender: AnyObject) { println("追加") // myItemsに追加. myItems.addObject("add Cell") // TableViewを再読み込み. myTableView.reloadData() } /* Cellを挿入または削除しようとした際に呼び出される */ func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { // 削除のとき. if editingStyle == UITableViewCellEditingStyle.Delete { println("削除") // 指定されたセルのオブジェクトをmyItemsから削除する. myItems.removeObjectAtIndex(indexPath.row) // TableViewを再読み込み. myTableView.reloadData() } } } このように書いたのですが、ビルドの時に The myCell outlet from the ViewController to the UITableViewCell is invalid. Outlets cannot be connected to repeating content. というエラーがでてビルドできません。ひとつにつき何度もOutletできませんという意味にしかとれず困っています。毎度すいません。
退会済みユーザー

退会済みユーザー

2015/05/13 07:17

storyboardで追加するのは、Table Viewだけですので、 @IBOutlet weak var myTableView: UITableView! の1行だけでいいはずなのですが…… 下記のブログエントリーをご参考ください。 http://dev.classmethod.jp/references/ios-8-xcode-6-swift-listview/ なお、この場合も as演算子のエクスクラメーションマーク(!)は不要です。 どうしても storyboardを使わなければならない理由がないのであれば、参考にされたサイトのソースコードを改変されたほうがよいかもしれませんね。
summer

2015/05/13 08:52

回答ありがとうございます。 なんとかアドバイスをもとに import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { //@IBOutlet weak var myUIView: UIView! @IBOutlet weak var myTableView: UITableView! //@IBOutlet weak var myCell: UITableViewCell! let myItems: NSMutableArray = ["TEST1", "TEST2", "TEST3"] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. myTableView.delegate = self myTableView.dataSource = self self.navigationItem.rightBarButtonItem = self.editButtonItem() myTableView.allowsSelectionDuringEditing = true } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* Cellの総数を返す (実装必須) */ func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return myItems.count } /* Cellに値を設定する (実装必須) */ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell // Cellに値を設定. cell.textLabel?.text = "\(myItems[indexPath.row])" return cell } /* 編集ボタンが押された際に呼び出される */ override func setEditing(editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) // TableViewを編集可能にする myTableView.setEditing(editing, animated: true) // 編集中のときのみaddButtonをナビゲーションバーの左に表示する if editing { println("編集中") let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "addCell:") self.navigationItem.setLeftBarButtonItem(addButton, animated: true) } else { println("通常モード") self.navigationItem.setLeftBarButtonItem(nil, animated: true) } } /* addButtonが押された際呼び出される */ func addCell(sender: AnyObject) { println("追加") // myItemsに追加. myItems.addObject("add Cell") // TableViewを再読み込み. myTableView.reloadData() } /* Cellを挿入または削除しようとした際に呼び出される */ func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { // 削除のとき. if editingStyle == UITableViewCellEditingStyle.Delete { println("削除") // 指定されたセルのオブジェクトをmyItemsから削除する. myItems.removeObjectAtIndex(indexPath.row) // TableViewを再読み込み. myTableView.reloadData() } } } こちらのコードによりほぼ望み通りの結果が得られているのですが func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { // 削除のとき. if editingStyle == UITableViewCellEditingStyle.Delete { println("削除") // 指定されたセルのオブジェクトをmyItemsから削除する. myItems.removeObjectAtIndex(indexPath.row) // TableViewを再読み込み. myTableView.reloadData() } } こちらの部分のコードが機能してくれません。追加はできるし、エディットボタンを押せば編集モードになるのですが、マイナスの記号を押してもDeleteが表示されず、セルを削除できません。 いろいろおつきあいしていただきましてありがとうございます。あともう少し力を貸してください。
summer

2015/05/13 13:33

いろいろ試しているのですが、どうやら func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { // 削除のとき. if editingStyle == UITableViewCellEditingStyle.Delete { println("削除") // 指定されたセルのオブジェクトをmyItemsから削除する. myItems.removeObjectAtIndex(indexPath.row) // TableViewを再読み込み. myTableView.reloadData() } } このコードが機能してないというよりはEdit後のDeleteが表示されないのが原因のようです。storyboardでcellを選択してaccessaryでdisclosure indicatorを選択しても反映しないのでどうやらセル自体に問題がありそうなのですが・・・。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問