ios - How to jump to any Cell in a UICollectionView by using a custom layout? -


i have 40 cells in horizontal uicollectionview , button.

i can jump cell number 5 cell number 10 when click on button.

but want go further cell ( example 5 25 ),

it doesn't work, , instead goes 0.

code:

func setvalue(value: int, animated: bool) {    self.value = value    if let row = find(values, value) {    let indexpath = nsindexpath(forrow: row, insection: 0)    selectedcell = collectionview.cellforitematindexpath(indexpath) as? steppercell    collectionview.scrolltoitematindexpath(indexpath, atscrollposition: .centeredhorizontally, animated: animated)   }  }  override func preparelayout() {    let start = int(collectionview!.bounds.size.width * 0.5) - statics.width / 2    let length = collectionview!.numberofitemsinsection(0)  if cache.isempty && length > 0 {    item in 0..<length {      let indexpath = nsindexpath(foritem: item, insection: 0)      let attributes = uicollectionviewlayoutattributes(forcellwithindexpath: indexpath)      attributes.frame = cgrect(       x: start + (statics.width + statics.padding) * indexpath.item,       y: 0,       width: statics.width,       height: statics.height     )     cache.append(attributes)   }   contentwidth = cgrectgetmaxx(cache.last!.frame) + cgfloat(start)    }  }   override func layoutattributesforelementsinrect(rect: cgrect) -> [anyobject]? {    var layoutattributes = [uicollectionviewlayoutattributes]()    attributes in cache {       if cgrectintersectsrect(attributes.frame, rect) {         layoutattributes.append(attributes)     }   }   return layoutattributes   }  } 

enter image description here

i think can dispense row logic. after - using passed int. if if let clause fails, won't scroll.

also, drop call cellforitematindexpath - not need (you not using either).

i guess perhaps should use nsindexpath(foritem, insection) rather nsindexpath(forrow, insection). (table view uses row, collectionview uses item.)

collectionview.scrolltoitematindexpath(       nsindexpath(foritem: value, insection: 0),        atscrollposition: .centeredhorizontally, animated: true) 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -