ios - Xamarin UICollectionView some cells disappear after scrolling down and back -
i have problem uicollectionview, cells... when i'm scrolling down , cells empty. disappear this:
my viewdidload() in ordersview:
public override void viewdidload() { title = "my orders.."; base.viewdidload(); orderscollectionview.frame = new cgrect(orderscollectionview.frame.x, orderscollectionview.frame.y, orderscollectionview.frame.width, orderscollectionview.frame.height); orderscollectionview.scrollenabled = true; var layout = new uicollectionviewflowlayout { itemsize = new cgsize() { width = orderscollectionview.bounds.width, height = 80 }, minimuminteritemspacing = 0, minimumlinespacing = 2, scrolldirection = uicollectionviewscrolldirection.vertical }; orderscollectionview.setcollectionviewlayout(layout, true); this.clearbindings(_source); _source = new orderscollectionviewsource(orderscollectionview); this.addbindings(new dictionary<object, string> { { _source, "itemssource orders; selecteditem selectedorder; selectionchangedcommand showorderdetailcommand" } }); orderscollectionview.source = _source; orderscollectionview.reloaddata(); mvx.resolve<imvxmessenger>().subscribeonmainthread<ordersloadedmessage>(mess => { this.clearbindings(_source); _source = new orderscollectionviewsource(orderscollectionview); this.addbindings(new dictionary<object, string> { { _source, "itemssource orders; selecteditem selectedorder; selectionchangedcommand showorderdetailcommand" } }); orderscollectionview.source = _source; orderscollectionview.reloaddata(); }, mvxreference.strong); initializerefreshcontrol(); viewmessages.viewcreatedmessages(this, "ordersview", viewmodel); }
orderscollectionviewsource:
public class orderscollectionviewsource : mvxcollectionviewsource { private static readonly nsstring cellidentifier = new nsstring("orderscollectionviewcell"); public orderscollectionviewsource(uicollectionview collectionview) : base(collectionview) { collectionview.registerclassforcell(typeof(orderscollectionviewcell), cellidentifier); } protected override uicollectionviewcell getorcreatecellfor(uicollectionview collectionview, nsindexpath indexpath, object item) { var cell = (uicollectionviewcell)collectionview.dequeuereusablecell(cellidentifier, indexpath); cell.backgroundcolor = uicolor.fromrgb(237, 237, 237); return cell; } }
orderscollectionviewcell:
public class orderscollectionviewcell : mvxcollectionviewcell { [export("initwithframe:")] public orderscollectionviewcell(cgrect frame) : base(frame) { layer.backgroundcolor = new cgcolor(220, 25, 25, 255); //left var labelname = new uilabel(); labelname.frame = new cgrect(10f, 15f, (float)(bounds.width / 2 + 20), 20f); labelname.font = uifont.preferredheadline; labelname.textcolor = uicolor.fromrgba(90, 24, 24, 255); labelname.autoresizingmask = uiviewautoresizing.flexiblewidth | uiviewautoresizing.flexibleleftmargin | uiviewautoresizing.flexiblerightmargin; labelname.text = "text"; var labelpickupdate = new uilabel(); labelpickupdate.frame = new cgrect(10f, 45f, (float)(bounds.width / 2 - 10), 20f); labelpickupdate.font = uifont.preferredbody; labelpickupdate.textcolor = uicolor.black; labelpickupdate.autoresizingmask = uiviewautoresizing.flexiblewidth | uiviewautoresizing.flexibleleftmargin | uiviewautoresizing.flexiblerightmargin; //right var labelprice = new uilabel(); labelprice.frame = new cgrect((float)(bounds.width / 2), 45f, (float)(bounds.width / 2 - 10), 20f); labelprice.textalignment = uitextalignment.right; labelprice.font = uifont.preferredbody; labelprice.textcolor = uicolor.black; labelprice.autoresizingmask = uiviewautoresizing.flexiblewidth | uiviewautoresizing.flexibleleftmargin | uiviewautoresizing.flexiblerightmargin; var labelstatus = new uilabel(); labelstatus.frame = new cgrect((float)(bounds.width / 2), 15f, (float)(bounds.width / 2 - 10f), 20f); labelstatus.textalignment = uitextalignment.right; labelstatus.font = uifont.preferredheadline; labelstatus.textcolor = uicolor.fromrgba(15, 113, 10, 255); labelstatus.autoresizingmask = uiviewautoresizing.flexiblewidth | uiviewautoresizing.flexibleleftmargin | uiviewautoresizing.flexiblerightmargin; add(labelname); add(labelpickupdate); add(labelstatus); add(labelprice); this.delaybind(() => { var set = this.createbindingset<orderscollectionviewcell, order>(); set.bind(labelpickupdate).for(q => q.text).to(vm => vm.pickupdate).withconversion("stringdatetimeconverter"); set.bind(labelstatus).for(q => q.text).to(vm => vm.orderstatus); set.bind(labelprice).to(vm => vm.sum).withconversion("creditconverter"); set.apply(); }); } }
i found here problems in xcode.. tried still doesn't work well.
edit - solution:
protected override uicollectionviewcell getorcreatecellfor(uicollectionview collectionview, nsindexpath indexpath, object item) { var cell = (uicollectionviewcell)collectionview.dequeuereusablecell(cellidentifier, indexpath); cell.backgroundcolor = uicolor.fromrgb(237, 237, 237); var order = (order) item; var test = (orderscollectionviewcell) cell; test.labelname.text = "name"; test.labelpickupdate.text = stringconvertor.stringdatetime(order.pickupdate); test.labelprice.text = stringconvertor.priceconvertor(order.sum); test.labelstatus.text = order.orderstatus; return test; }
i had similar problem. because table reuses cells. dequeuereusablecellwithidentifier
reuse same cell references when scrolling , may not preserve text intended display.
your getorcreatecellfor
function contains call dequeuereusablecell
. should set cell's text within function (e.g. cell.labelname.text = "my text here"
).
protected override uicollectionviewcell getorcreatecellfor(uicollectionview collectionview, nsindexpath indexpath, object item) { var cell = (uicollectionviewcell)collectionview.dequeuereusablecell(cellidentifier, indexpath); cell.backgroundcolor = uicolor.fromrgb(237, 237, 237); cell.labelname.text = "your label here" cell.labelpickupdate.text = "your pickup date here" cell.labelprice.text = "your price here" cell.labelstatus.text = "your status here" return cell; }
please see answer below full explanation. let me know if have questions.
Comments
Post a Comment