ios - My CollectionViewCell is getting into a mess -


you can download project files here

look @ cells.
@ first normal. normal pic mess after scroll down , below. abnormal pic

i don't know why.. , have wasted time few days.
implemented method: collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize

source code: settingsviewcontroller.swift

import uikit  class supplementaryview: uicollectionreusableview {     var imageview = uiimageview()     override init(frame: cgrect) {         super.init(frame: frame)          self.addsubview(imageview)         imageview.translatesautoresizingmaskintoconstraints = false     }      required init?(coder adecoder: nscoder) {         fatalerror("init(coder:) has not been implemented")     } }  class settingscell: uicollectionviewcell {     var imageview = uiimageview()     var celllabel = uilabel()     var textfield = uitextfield()      override init(frame: cgrect) {         super.init(frame: frame)          self.contentview.addsubview(celllabel)         celllabel.translatesautoresizingmaskintoconstraints = false         self.contentview.addsubview(textfield)         textfield.translatesautoresizingmaskintoconstraints = false         self.contentview.addsubview(imageview)         imageview.translatesautoresizingmaskintoconstraints = false     }      required init?(coder adecoder: nscoder) {         fatalerror("init(coder:) has not been implemented")     } }  class settingsviewcontroller: uiviewcontroller {      let uidesign = uidesign()     let layout = uicollectionviewflowlayout()     var collectionview: uicollectionview!      let reuseidentifier = "reuseidentifier"     let headeridentifier = "headeridentifier"     let sections = ["image", "image"]     let cells = ["label", "textfield", "image", "label", "textfield", "image", "label", "textfield", "image", "label", "textfield", "image"]      var collectionviewwidth: cgfloat = 0      override func viewdidload() {         super.viewdidload()          setupui()          collectionview.datasource = self         collectionview.delegate = self      }       func setupui() {          //  navi bar         self.title = "math avengers - settings"         self.navigationitem.leftbarbuttonitem = uibarbuttonitem(title: "이전 단계로", style: .plain, target: self, action: #selector(self.leftbarbuttonpressed))         self.navigationitem.rightbarbuttonitem = uibarbuttonitem(title: "다음 단계로", style: .plain, target: self, action: #selector(self.nextbuttonpressed))          layout.scrolldirection = .vertical         layout.headerreferencesize = (uiimage(named: "name")?.size)!         layout.sectionheaderspintovisiblebounds = true         layout.minimumlinespacing = 10         layout.minimuminteritemspacing = 0         layout.sectioninset = uiedgeinsetszero          collectionview = uicollectionview(frame: self.view.frame, collectionviewlayout: layout)         collectionview.backgroundcolor = uicolor.yellowcolor()         collectionview.collectionviewlayout = layout         collectionview.translatesautoresizingmaskintoconstraints = false         self.view.addsubview(collectionview)         collectionview.registerclass(settingscell.self, forcellwithreuseidentifier: reuseidentifier)         collectionview.registerclass(supplementaryview.self, forsupplementaryviewofkind: uicollectionelementkindsectionheader, withreuseidentifier: headeridentifier)          collectionviewwidth = collectionview.frame.size.width          let viewsdictionary = ["collectionview": collectionview]         self.view.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:|[collectionview]|",             options: .alignallcenterx, metrics: nil, views: viewsdictionary))         self.view.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|[collectionview]|",             options: .alignallcentery, metrics: nil, views: viewsdictionary))      }      func leftbarbuttonpressed() {      }      func nextbuttonpressed() {      }       override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     } }  extension settingsviewcontroller: uitextfielddelegate {      func textfieldshouldreturn(textfield: uitextfield) -> bool {         textfield.resignfirstresponder()         self.nextbuttonpressed()         return true     } }   extension settingsviewcontroller: uicollectionviewdatasource {     func numberofsectionsincollectionview(collectionview: uicollectionview) -> int {         return sections.count     }      func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int {         return cells.count     }      func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell {         let cell = collectionview.dequeuereusablecellwithreuseidentifier(reuseidentifier, forindexpath: indexpath) as! settingscell          switch cells[indexpath.row] {         case "label":              //cell.celllabel.hidden = false             cell.celllabel.frame = cell.contentview.frame             cell.celllabel.text = "이름을 적어주세요.\(indexpath.row)"             uidesign.setlabellayout(cell.celllabel, fontsize: 40)              break          case "textfield":              cell.textfield.frame = cell.contentview.frame             cell.textfield.text = "007_\(indexpath.row)"             uidesign.settextfieldlayout(cell.textfield, fontsize: 40)              break          case "image":              cell.imageview.frame = cell.contentview.frame             cell.imageview.image = uiimage(named: "next")             cell.imageview.contentmode = .scaleaspectfit              break          default:             debugprint("default")             break         }         return cell     }      // 섹션 헤더 설정     func collectionview(collectionview: uicollectionview, viewforsupplementaryelementofkind kind: string, atindexpath indexpath: nsindexpath) -> uicollectionreusableview {         var headerview: supplementaryview?         if (kind == uicollectionelementkindsectionheader) {             headerview = collectionview.dequeuereusablesupplementaryviewofkind(kind, withreuseidentifier: headeridentifier, forindexpath: indexpath) as? supplementaryview             let image = indexpath.row % 2 == 0 ? uiimage(named: "name") : uiimage(named: "age")             headerview?.imageview.image = image             headerview?.imageview.contentmode = .scaleaspectfit             headerview?.imageview.frame = cgrectmake(0, 0, collectionviewwidth, image!.size.height)             headerview?.backgroundcolor = uicolor(red: 1, green: 1, blue: 1, alpha: 0.5)         }         return headerview!     }      func collectionview(collectionview: uicollectionview, willdisplaycell cell: uicollectionviewcell, foritematindexpath indexpath: nsindexpath) {         let cell = cell as! settingscell          switch cells[indexpath.row] {         case "label":             cell.backgroundcolor = uicolor(red: 0, green: 1, blue: 0, alpha: 0.5)             cell.celllabel.hidden = false             cell.imageview.hidden = true             cell.textfield.hidden = true             break          case "textfield":             cell.backgroundcolor = uicolor(red: 1, green: 0.5, blue: 0, alpha: 0.5)             cell.textfield.hidden = false             cell.celllabel.hidden = true             cell.imageview.hidden = true             break          case "image":             cell.backgroundcolor = uicolor(red: 0, green: 0.5, blue: 1, alpha: 0.5)             cell.imageview.hidden = false             cell.celllabel.hidden = true             cell.textfield.hidden = true             break          default:             break          }     } }  extension settingsviewcontroller: uicollectionviewdelegateflowlayout {     func collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize {          switch cells[indexpath.row] {         case "label":             return cgsizemake(collectionviewwidth, 200)          case "image":             let img = uiimage(named: "next")             return cgsizemake(collectionviewwidth, 200) //(img?.size.height)!)          case "textfield":             return cgsizemake(collectionviewwidth-200, 100)          default:             return cgsizemake(collectionviewwidth, 200)          }     } } 

i solved problem autolayout below.
1. blocked statements "//cell.celllabel.frame = cell.contentview.frame"
2. added constraints.

func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell {     let cell = collectionview.dequeuereusablecellwithreuseidentifier(reuseidentifier, forindexpath: indexpath) as! settingscell      switch cells[indexpath.row] {     case "label":          //cell.celllabel.hidden = false         //cell.celllabel.frame = cell.contentview.frame         cell.celllabel.text = "이름을 적어주세요.\(indexpath.row)"         uidesign.setlabellayout(cell.celllabel, fontsize: 40)          let viewsdictionary = ["celllabel": cell.celllabel]         cell.contentview.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:|-[celllabel]-|", options: .alignallcenterx, metrics: nil, views: viewsdictionary))         cell.contentview.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|-[celllabel]-|", options: .alignallcentery, metrics: nil, views: viewsdictionary))          break      case "textfield":          //cell.textfield.frame = cell.contentview.frame         cell.textfield.text = "007_\(indexpath.row)"         uidesign.settextfieldlayout(cell.textfield, fontsize: 40)         cell.textfield.delegate = self          let viewsdictionary = ["textfield": cell.textfield]         cell.contentview.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:|-[textfield]-|", options: .alignallcenterx, metrics: nil, views: viewsdictionary))         cell.contentview.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|-[textfield]-|", options: .alignallcentery, metrics: nil, views: viewsdictionary))          break      case "image":          //cell.imageview.frame = cell.contentview.frame         cell.imageview.image = uiimage(named: "next")         cell.imageview.contentmode = .scaleaspectfit          let viewsdictionary = ["imageview": cell.imageview]         cell.contentview.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:|-[imageview]-|", options: .alignallcenterx, metrics: nil, views: viewsdictionary))         cell.contentview.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|-[imageview]-|", options: .alignallcentery, metrics: nil, views: viewsdictionary))          break      default:         debugprint("default")         break     }     return cell } 

Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -