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

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

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

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

Q&A

1回答

3539閲覧

swiftにおいて、スクロールするとUITableViewのAuto Layoutが崩れてしまいます。

taiking

総合スコア11

Swift

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

0グッド

0クリップ

投稿2015/07/13 02:15

UITableViewをカスタマイズしていますが、そのなかのAuto Layoutがスクロールすると崩れたり、元に戻ったりします。原因はUITableViewCellの再利用が関係していると思いましたが、イマイチその概念がわからず、改善策を思いつくことができませんでした。改善できる方法があれば教えていただきたいです。よろしくお願いします。
コードは下記に示したものが全貌です。

lang

1 2class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{ 3 4 let tableView = UITableView() 5 6 override func viewDidLoad() { 7 super.viewDidLoad() 8 9 tableView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height) 10 tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "MyCell") 11 tableView.dataSource = self 12 tableView.delegate = self 13 14 self.view.addSubview(tableView) 15 } 16 17 18 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 19 return 15 20 } 21 22 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 23 var cell:TableViewCell! = tableView.dequeueReusableCellWithIdentifier("MyCell") as! TableViewCell 24 cell.label.text = "あいうえおかきくけこさしすせとたちつてとなにぬぬのまみむめも" 25 if indexPath.row % 2 == 0{ 26 cell.makeLayout(true) 27 }else{ 28 cell.makeLayout(false) 29 } 30 return cell 31 32 } 33 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 34 return 70 35 } 36} 37 38class TableViewCell: UITableViewCell{ 39 required init(coder aDecoder: NSCoder){ 40 super.init(coder: aDecoder) 41 } 42 43 var backColorView = UIView() 44 var label: UILabel = UILabel() 45 var send = true 46 47 override init(style: UITableViewCellStyle, reuseIdentifier: String?){ 48 super.init(style: style, reuseIdentifier: reuseIdentifier) 49 label.numberOfLines = 0 50 label.lineBreakMode = NSLineBreakMode.ByWordWrapping 51 label.text = "あいうえおかきくけこさしすせとたちつてとなにぬぬのまみむめも" 52 53 backColorView.backgroundColor = UIColor.yellowColor() 54 55 contentView.addSubview(backColorView) 56 backColorView.addSubview(label) 57 58 backColorView.setTranslatesAutoresizingMaskIntoConstraints(false) 59 label.setTranslatesAutoresizingMaskIntoConstraints(false) 60 61 } 62 63 func makeLayout(sendBool: Bool){ 64 label.preferredMaxLayoutWidth = 218 65 var layoutAttribute: NSLayoutAttribute 66 var layoutConstant: CGFloat = 0 67 send = sendBool 68 if sendBool { 69 layoutAttribute = NSLayoutAttribute.Left 70 layoutConstant = 10 71 contentView.addConstraint(NSLayoutConstraint(item: backColorView, attribute: layoutAttribute, relatedBy: .Equal, toItem: contentView, attribute: layoutAttribute, multiplier: 1, constant: layoutConstant)) 72 73 } else { // outgoing 74 layoutAttribute = NSLayoutAttribute.Right 75 layoutConstant = -10 76 contentView.addConstraint(NSLayoutConstraint(item: backColorView, attribute: layoutAttribute, relatedBy: .Equal, toItem: contentView, attribute: layoutAttribute, multiplier: 1, constant: layoutConstant)) 77 } 78 backColorView.addConstraint(NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: backColorView, attribute: .CenterX, multiplier: 1, constant: 3)) 79 contentView.addConstraint(NSLayoutConstraint(item: backColorView, attribute: .Top, relatedBy: .Equal, toItem: contentView, attribute: .Top, multiplier: 1, constant: 4.5)) 80 backColorView.addConstraint(NSLayoutConstraint(item: backColorView, attribute: .Width, relatedBy: .Equal, toItem: label, attribute: .Width, multiplier: 1, constant: 30)) 81 contentView.addConstraint(NSLayoutConstraint(item: backColorView, attribute: .Bottom, relatedBy: .Equal, toItem: contentView, attribute: .Bottom, multiplier: 1, constant: -4.5)) 82 83 backColorView.addConstraint(NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: backColorView, attribute: .CenterY, multiplier: 1, constant: -0.5)) 84 label.preferredMaxLayoutWidth = 218 85 backColorView.addConstraint(NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .Equal, toItem: backColorView, attribute: .Height, multiplier: 1, constant: -15)) 86 } 87 88}

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

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

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

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

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

guest

回答1

0

tableView.dequeueReusableCellWithIdentifier("MyCell")

ここってtableView.dequeueReusableCellWithIdentifier("MyCell" forIndexPath: indexPath)ってするとどうなりますか?

投稿2015/07/17 10:36

__moai

総合スコア264

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問