tableView alerttext
- 評価
- クリップ 1
- VIEW 517

退会済みユーザー
import UIKit
class ViewController: UIViewController,UITabBarDelegate,UITableViewDataSource {
@IBOutlet weak var mytableView: UITableView!
var Haru = ["cell 追加"]
var newname = String()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func addbutton(_ sender: Any) {
let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: {
(action:UIAlertAction!) -> Void in
if let textFields = alert.textFields {
for textField in textFields {
self.kei.insert(textField.text!, at:0)
//self.kei.append(textField.text!)
}
self.mytableView.reloadData()
}
})
alert.addAction(okAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(cancelAction)
alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
textField.placeholder = "テキスト"
})
alert.view.setNeedsLayout()
self.present(alert, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Haru.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell();
cell.textLabel?.text = kei[indexPath.row]
cell.textLabel?.textColor = UIColor.brown
cell.textLabel?.backgroundColor = UIColor.blue
cell.textLabel?.font = UIFont.systemFont(ofSize: 30)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath: IndexPath){
let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: {
(action:UIAlertAction!) -> Void in
if let textFields = alert.textFields {
for textField in textFields {
self.kei.insert(textField.text!, at:0)
//self.kei.append(textField.text!)
}
self.mytableView.reloadData()
}
})
alert.addAction(okAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(cancelAction)
alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
textField.placeholder = "テキスト"
})
alert.view.setNeedsLayout()
self.present(alert, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
kei.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
やりたいこと
他の人の質問を参考にしてcellをタップするとアラートテキストが表示され入力出来るようになったのですが理想としては[cell 追加]以外はタップしても何も入力できないようにしたいです。
func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
switch indexPath.row {
case 0:
return indexPath
// 選択不可にしたい場合は"nil"を返す
case 1:
return nil
default:
return indexPath
}
}
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if kei.count > (indexPath.row + 1) {
return nil
}
return indexPath
}
教えていただい通りにこのようにして見たのですがやっぱり[cell 追加]以外はタップしてもアラートテキストが出てくるのですがこの他にさらに別のメソッドが必要なでしょうか?
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
+1
@fuzzball さんの回答を補足すると、 indexPath
に選択したセルの位置が渡されるので、選択させたくないセルの位置だったら nil
を返す。そうでない場合は、 indexPath
をそのまま返すようにします。
IndexPath には section
と row
があって、今回 section
は一つしかないので、indexPath.row
がセルの位置と等しくなっています。
今回のコードだと、 一番下のセル が反応してほしくないので次のように判定する方法があります。
他にも考え方はあるので参考の一つとしてください。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = Haru[indexPath.row]
cell.selectionStyle = (Haru.count > (indexPath.row + 1)) ? .none : .blue
return cell
}
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if Haru.count > (indexPath.row + 1) {
return nil
}
return indexPath
}
参考
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
+1
選択させたくないセルは、UITableViewDelegateのtableView(_:willSelectRowAt:)
でnilを返せばいいです。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 89.99%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2017/09/21 13:38
let cell = UITableViewCell();
switch indexPath.row {
case 0:
return indexPath;
// 選択不可にしたい場合は"nil"を返す
case 1:
cell.textLabel?.text = kei[indexPath.row]
return nil;
default:
cell.textLabel?.text = kei[indexPath.row]
return nil;
}
}
早速教えていただいたことをやって見たのですがうまくできなかったのですがどこらへんが間違っているのか教えて頂けませんか?
2017/09/21 13:52
一番上のセル以外は選択不可になったと思いますが、そうなっていないですか?
また、このメソッドの中で cell を変更しようとするのは不適切で、不要な処理です。
2017/09/21 14:25
不適切な処理なんですね!勉強になりました。
2017/09/21 14:34
判定方法の考え方の一つを回答に追記しました。
2017/09/21 15:04
switch indexPath.row {
case 0:
if Haru.count > (indexPath.row + 1) {
return nil
}
return indexPath
// 選択不可にしたい場合は"nil"を返す
case 1:
return nil
default:
return nil;
}
}
このようにして見ましたが先ほどと結果が変わりませんでした。
もしかしたらコード的に元から出来ないんでしょうか?
2017/09/21 15:16
この式が switch indexPath.row の中にあるのは変だとは思いませんか?
メソッド中への記載を回答に追記しました。
2017/09/21 21:14
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath)
if indexPath.row == 1 {
// セルの選択不可にする
cell.selectionStyle = UITableViewCellSelectionStyle.None
} else {
// セルの選択を許可
cell.selectionStyle = UITableViewCellSelectionStyle.Blue
}
return cell
}
このようなコードもあったのですがこれは関係ありますでしょうか?
2017/09/21 23:12
回答のコードを参考に、自身のコードではどのセル番号を選択不可にするか、許可するか適切に設定できれば期待する実装ができると思います。
2017/09/22 08:55
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for:indexPath as IndexPath)
if kei.count > (indexPath.row + 1) {
// セルの選択不可にする
} else {
// セルの選択を許可
cell.selectionStyle = UITableViewCellSelectionStyle.blue
}
return cell
}
func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
switch indexPath.row {
case 0:
return indexPath;
// 選択不可にしたい場合は"nil"を返す
case 1:
return nil;
default:
return indexPath;
}
}
参考にしてこのようにして見たのですがやはり、[cell 追加]以外のcellをタップするとアラートテキストが出でしまいます。この場合やはり私のコードが適切じゃないんでしょうか?もしくこの部分以外のコードに問題があるのでしょうか?
2017/09/22 09:05
2017/09/22 10:20
2017/09/22 19:19 編集
func didSelectRowAtIndexPathにindexPathを加えたら大丈夫でした。
@ykwsさんのおかげで私のしたいことがなんとか出来ました。
本当にありがとうございました。
2017/09/24 02:55
2017/09/24 08:58