objective c - How to determine indexPath of currently visible UICollectionView cell -
i want display cell-specific text, can’t extricate visible cell collection view.
most helpful ones these:
how access uicollectionviewcell indexpath of cell in uicollectionview
detecting when ios uicollectioncell going off screen
a lower collection view (self addsubview) contains possible items. upper 1 (self addsubview) contains selections lower. user presses button (self addsubview again), lower view scrolls off oblivion , upper 1 expands such 1 cell fills screen. array-building , displaying works fine. no *.nibs.
what doesn’t work figuring out cell app presenting user.
try #1: first tried populating uilabel in parallel normal ops. press button , cellforitematindexpath:
if (cv.tag == upper_cv_tag) { uicollectionviewlayoutattributes *itemattributes = [uicollectionviewlayoutattributes layoutattributesforcellwithindexpath:indexpath]; nsindexpath *tempindexpath; tempindexpath = itemattributes.indexpath; nslog(@"cellforitem indexpath %@",indexpath); nslog(@"layout indexpath %@",tempindexpath); }
responds you’d expect:
cellforitem indexpath <blah blah> 2 indexes [0, 0] layout indexpath < > 2 indexes [0, 0] cellforitem indexpath < > 2 indexes [0, 1] layout indexpath < > 2 indexes [0, 1] cellforitem indexpath < > 2 indexes [0, 2] layout indexpath < > 2 indexes [0, 2]
when gyrations conclude, cell 0 correctly occupies center stage. unfortunately, description cell 2. scroll left / right, cells change accordingly, corresponding text not. remains constant: maps cell 2.
obviously i’m passing wrong index description array.
try #2: ok, fine. i’ll interrogate point know visible cell occupies , reason backward that.
cgpoint p = cgpointmake(512, 456); nsindexpath *thecellsindexpath = [self.upper_collectionview indexpathforitematpoint:p]; nslog(@"thecellsindexpath=%d",thecellsindexpath.item);
no cigar:
cellforitem indexpath < > 2 indexes [0, 0] thecellsindexpath=0 cellforitem indexpath < > 2 indexes [0, 1] thecellsindexpath=0 cellforitem indexpath < > 2 indexes [0, 2] thecellsindexpath=0
no change “0” when change 512,456 other points. no change “0” when use layoutattributes determine in view cell lies, in case. nope, cell really, big target. i’m not missing it.
try #3: maybe i’m working hard. i’ll trap goings-on inside
- (uicollectionviewlayoutattributes *)layoutattributesforitematindexpath:(nsindexpath *)indexpath { }
and roll that.
nope. never gets called.
try #4: since don’t know indexpath of single cell on screen (but collectionview does) suppose iterate through cells didenddisplayingcell until hit “bingo!” that’s crude , indicative of programmer error.
i’m glad collectionview logic buffers 3 cells. improves user experience or something. ends – on 0 – not array index last knew – @ 2.
what part of docs didn’t read?
what part of docs didn’t read?
uicollectionview
has method called indexpathsforvisibleitems
. make sure read through see else can use api.
Comments
Post a Comment