今回のサンプルでは、StoryBoardで以下のように、UITableViewの上にUITableViewCellを置いたものに対しての内容です。

▪️UITableViewを操作するために、UITableViewDataSource, UITableViewDelegateを継承
▪️StoryBoardで配置したTableViewをアシスタントエディタで紐付け、TableViewに表示するデータを定義
▪️TableViewを操作するために必須の以下の2メソッドを定義
セルの行数を返すメソッドで、データの数を返す。
セルの内容を設定するメソッドで、UITableViewCell上に配置したUITextFieldにTag番号1を設定していて、viewWithTagメソッドで取得する。
取得したTextFieldにデータを設定する。
▪️TableViewに新しいデータを追加
Navigation BarのAddボタンを紐付けして、「+」ボタンが押された時に呼ばれるようにする。
// セルに新しいデータを追加
@IBAction func tapAdd(sender: AnyObject) {
// myItemsに追加.
myItems.addObject("タイトルNEW")
// TableViewを再読み込み.
tableView.reloadData()
}
▪️TableViewを編集状態に変更
Navigation BarのEditボタンを紐付けして、「Edit」ボタンが押された時に呼ばれるようにする。
// セルの編集状態を変更
@IBAction func tapEdit(sender: AnyObject) {
if editing {
super.setEditing(false, animated: true)
tableView.setEditing(false, animated: true)
} else {
super.setEditing(true, animated: true)
tableView.setEditing(true, animated: true)
}
}
▪️TableViewからデータを削除
セルを追加や削除をする時に呼ばれるメソッドで、削除の場合の処理を書く。
// セルを追加or削除しようとした場合
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// 削除のとき.
if editingStyle == UITableViewCellEditingStyle.Delete {
// 指定されたセルのオブジェクトをmyItemsから削除する.
myItems.removeObjectAtIndex(indexPath.row)
// TableViewを再読み込み.
tableView.reloadData()
}
}
▪️TableViewの並び替え
以下のメソッドを定義するだけで並び替えが可能となる。
// セルの並び替えを有効にする
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
}
上記の設定をすることで、TableViewに対して、追加、削除、並び替えができるようになります!
以下がスクリーンショットです。



全体のソースコードは以下です。
実際の動作は以下のアプリを公開していますので試してみて下さい。

巻数メモ

▪️UITableViewを操作するために、UITableViewDataSource, UITableViewDelegateを継承
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {▪️StoryBoardで配置したTableViewをアシスタントエディタで紐付け、TableViewに表示するデータを定義
@IBOutlet weak var tableView: UITableView!
let myItems: NSMutableArray = ["タイトル1", "タイトル2", "タイトル3"]▪️TableViewを操作するために必須の以下の2メソッドを定義
セルの行数を返すメソッドで、データの数を返す。
// セルの行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myItems.count
}セルの内容を設定するメソッドで、UITableViewCell上に配置したUITextFieldにTag番号1を設定していて、viewWithTagメソッドで取得する。
取得したTextFieldにデータを設定する。
// セルの設定
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("SampleCell") as! UITableViewCell
// tag1を取得
var titleText = cell.viewWithTag(1) as! UITextField
titleText.text = "\(myItems[indexPath.row])"
return cell
}▪️TableViewに新しいデータを追加
Navigation BarのAddボタンを紐付けして、「+」ボタンが押された時に呼ばれるようにする。
// セルに新しいデータを追加
@IBAction func tapAdd(sender: AnyObject) {
// myItemsに追加.
myItems.addObject("タイトルNEW")
// TableViewを再読み込み.
tableView.reloadData()
}
▪️TableViewを編集状態に変更
Navigation BarのEditボタンを紐付けして、「Edit」ボタンが押された時に呼ばれるようにする。
// セルの編集状態を変更
@IBAction func tapEdit(sender: AnyObject) {
if editing {
super.setEditing(false, animated: true)
tableView.setEditing(false, animated: true)
} else {
super.setEditing(true, animated: true)
tableView.setEditing(true, animated: true)
}
}
▪️TableViewからデータを削除
セルを追加や削除をする時に呼ばれるメソッドで、削除の場合の処理を書く。
// セルを追加or削除しようとした場合
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// 削除のとき.
if editingStyle == UITableViewCellEditingStyle.Delete {
// 指定されたセルのオブジェクトをmyItemsから削除する.
myItems.removeObjectAtIndex(indexPath.row)
// TableViewを再読み込み.
tableView.reloadData()
}
}
▪️TableViewの並び替え
以下のメソッドを定義するだけで並び替えが可能となる。
// セルの並び替えを有効にする
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
}
上記の設定をすることで、TableViewに対して、追加、削除、並び替えができるようになります!
以下がスクリーンショットです。



全体のソースコードは以下です。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let myItems: NSMutableArray = ["タイトル1", "タイトル2", "タイトル3"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
// セルに新しいデータを追加
@IBAction func tapAdd(sender: AnyObject) {
// myItemsに追加.
myItems.addObject("タイトルNEW")
// TableViewを再読み込み.
tableView.reloadData()
}
// セルの編集状態を変更
@IBAction func tapEdit(sender: AnyObject) {
if editing {
super.setEditing(false, animated: true)
tableView.setEditing(false, animated: true)
} else {
super.setEditing(true, animated: true)
tableView.setEditing(true, animated: true)
}
}
// セルの行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myItems.count
}
// セルの設定
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("SampleCell") as! UITableViewCell
// tag1を取得
var titleText = cell.viewWithTag(1) as! UITextField
titleText.text = "\(myItems[indexPath.row])"
return cell
}
// セルを追加or削除しようとした場合
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// 削除のとき.
if editingStyle == UITableViewCellEditingStyle.Delete {
// 指定されたセルのオブジェクトをmyItemsから削除する.
myItems.removeObjectAtIndex(indexPath.row)
// TableViewを再読み込み.
tableView.reloadData()
}
}
// セルの並び替えを有効にする
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
実際の動作は以下のアプリを公開していますので試してみて下さい。

巻数メモ