swift - iOS Cell Reusing Changing Colors -


i know common problem, researched not find problem.

my code:

func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let row = indexpath.row      let cell1=tableview.dequeuereusablecellwithidentifier(textcellidentifier) as! messagecell      cell1.setcell(swiftblogs[row])      return cell1  } 

messagecell.swift

func setcell(message:message) {     messagelabel.text = message.text     if message.incoming != (tag == incomingtag) {         var layoutattribute: nslayoutattribute         var layoutconstant: cgfloat          if message.incoming {             tag = incomingtag             bubbleimageview.image = bubbleimage.incoming             bubbleimageview.highlightedimage = bubbleimage.incominghighlighed             messagelabel.textcolor = uicolor.blackcolor()             layoutattribute = .left             layoutconstant = 10         } else { // outgoing             tag = outgoingtag              if message.issent == 1 {                 bubbleimageview.image = bubbleimage.outgoing             }             if message.issent == 0 {                 bubbleimageview.image = bubbleimage.notyetsent             }             bubbleimageview.highlightedimage = bubbleimage.outgoinghighlighed             messagelabel.textcolor = uicolor.whitecolor()             layoutattribute = .right             layoutconstant = -10         }            let constraints: nsarray = contentview.constraints         let indexofconstraint = constraints.indexofobjectpassingtest { (constraint, idx, stop) in             return (constraint.firstitem as! uiview).tag == bubbletag && (constraint.firstattribute == nslayoutattribute.left || constraint.firstattribute == nslayoutattribute.right)         }         contentview.removeconstraint(constraints[indexofconstraint] as! nslayoutconstraint)         contentview.addconstraint(nslayoutconstraint(item: bubbleimageview, attribute: layoutattribute, relatedby: .equal, toitem: contentview, attribute: layoutattribute, multiplier: 1, constant: layoutconstant))     }  } 

let me explain code: can see in setcell if message.incoming true cell should on left side. if false cell should on right side. this working without problem. can see, have 1 more if statement. if message.issent == 1 changing image property of bubbleimageview , problem starts here. bubbleimage.outgoing blue image , bubbleimage.notyetsent red image. when start scrolling cell colors changing issent value not changing.

i have no problem first if statement (message.incoming check.) everythings looks great colors changing. how can resolve problem?

example:

before scroll:

enter image description here

after scroll:

enter image description here

i'm little unclear on how you're using tag, line:

if message.incoming != (tag == incomingtag) { 

will prevent reaching code:

        if message.issent == 1 {             bubbleimageview.image = bubbleimage.outgoing         }         if message.issent == 0 {             bubbleimageview.image = bubbleimage.notyetsent         } 

in case of cell reuse. code needs run every time set cell , cell outgoing message. consider moving part of setup outside of first conditional:

if message.incoming != (tag == incomingtag) {     // setup code  }  if !message.incoming {     if message.issent == 1 {         bubbleimageview.image = bubbleimage.outgoing     }     if message.issent == 0 {         bubbleimageview.image = bubbleimage.notyetsent     } } 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -