0

I am currently working on a socket based application in which I have to change color of UIlabel inside custom UITableView cell on the basis of comparing previous value of label with current value received from socket.

I take two string variables also in custom cell to check previous and current value. Here is cellFor row at index path code...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    MWCell *mwCell=(MWCell*)[tableView dequeueReusableCellWithIdentifier:@"mwCellIdentifier"];

    if (mwCell==nil) {

        NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"MWCell" owner:self options:nil];

        for (id temp in topLevelObjects) {
            if ([temp isKindOfClass:[MWCell class]]) {
                mwCell=(MWCell*)temp;
            }
        }
    }

    NSDictionary *tempDic=[watchScriptArray objectAtIndex:indexPath.row];

    if ([[tempDic valueForKey:@"expirydate"] valueForKey:@"text"]) {
        mwCell.companyName.text=[NSString stringWithFormat:@"%@-%@",[[tempDic valueForKey:@"symbolname"] valueForKey:@"text"],[[tempDic valueForKey:@"expirydate"] valueForKey:@"text"]];
    }else{
        mwCell.companyName.text=[NSString stringWithFormat:@"%@",[[tempDic valueForKey:@"symbolname"] valueForKey:@"text"]];
    }

    [mwCell.companyName setTextColor:t.formfgColor];
    [mwCell.companyName setShadowColor:t.formShadowColor];


    if (![mwCell.buyRate.text isEqualToString:@""]) {
        float preVal=[mwCell.prev_br floatValue];
        float nxtVal=[[[tempDic valueForKey:@"bestbuyprice"] valueForKey:@"text"] floatValue];

        if (nxtVal>preVal) {
            [mwCell.buyRate setBackgroundColor:t.socketHighbgColor];
            mwCell.buyRate.textColor=t.socketHighfgColor;
        }

        if (nxtVal<preVal){
            [mwCell.buyRate setBackgroundColor:t.socketLowbgColor];
            mwCell.buyRate.textColor=t.socketLowfgColor;
        }
    }else{
        [mwCell.buyRate setBackgroundColor:t.socketNormalbgColor];
        mwCell.buyRate.textColor=t.socketNormalfgColor;
    }

    if (![mwCell.sellRate.text isEqualToString:@""]) {
        float preVal=[mwCell.sellRate.text floatValue];
        float nxtVal=[[[tempDic valueForKey:@"bestsellprice"] valueForKey:@"text"] floatValue];

        if (nxtVal>preVal) {
            [mwCell.sellRate setBackgroundColor:t.socketHighbgColor];
            [mwCell.sellRate setTextColor:t.socketHighfgColor];
        }

        if (nxtVal<preVal) {
            [mwCell.sellRate setBackgroundColor:t.socketLowbgColor];
            [mwCell.sellRate setTextColor:t.socketLowfgColor];
        }
    }else{
        [mwCell.sellRate setBackgroundColor:t.socketNormalbgColor];
        mwCell.sellRate.textColor=t.socketNormalfgColor;
    }

    mwCell.buyRate.textAlignment=NSTextAlignmentCenter;
    mwCell.sellRate.textAlignment=NSTextAlignmentCenter;
    mwCell.buyRate.text=[[tempDic valueForKey:@"bestbuyprice"] valueForKey:@"text"];
    mwCell.sellRate.text=[[tempDic valueForKey:@"bestsellprice"] valueForKey:@"text"];
    mwCell.prev_br=[[tempDic valueForKey:@"bestbuyprice"] valueForKey:@"text"];
    mwCell.pre_sr=[[tempDic valueForKey:@"bestsellprice"] valueForKey:@"text"];

    [mwCell.buyRate setShadowOffset:CGSizeMake(0, 0)];
    [mwCell.sellRate setShadowOffset:CGSizeMake(0, 0)];

    if ([mwCell.buyRate.text floatValue]<0) {
        [mwCell.buyRate setBackgroundColor:t.socketLowbgColor];
        [mwCell.buyRate setTextColor:t.socketLowfgColor];
    }

    if ([mwCell.sellRate.text floatValue]<0) {
        [mwCell.sellRate setBackgroundColor:t.socketLowbgColor];
        [mwCell.sellRate setTextColor:t.socketLowfgColor];
    }

    return mwCell;
}

I have change my code to check with previous array values with current array values instead of checking current array with text in cell.But this also doesn't seems to work properly.On scrolling it fills other row color also.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MWCell *mwCell=(MWCell*)[tableView dequeueReusableCellWithIdentifier:@"mwCellIdentifier"];

    if (mwCell==nil) {

        NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"MWCell" owner:self options:nil];

        for (id temp in topLevelObjects) {
            if ([temp isKindOfClass:[MWCell class]]) {

                mwCell=(MWCell*)temp;

                mwCell.buyRate.backgroundColor=[UIColor whiteColor];
                mwCell.sellRate.backgroundColor=[UIColor whiteColor];

                mwCell.buyRate.textColor=[UIColor blackColor];
                mwCell.sellRate.textColor=[UIColor blackColor];
            }
        }
    }

    NSDictionary *tempDic=[watchScriptArray objectAtIndex:indexPath.row];
    NSDictionary *prevDict=[prevScriptArray objectAtIndex:indexPath.row];

    if ([[tempDic valueForKey:@"expirydate"] valueForKey:@"text"]) {
        mwCell.companyName.text=[NSString stringWithFormat:@"%@-%@",[[tempDic valueForKey:@"symbolname"] valueForKey:@"text"],[[tempDic valueForKey:@"expirydate"] valueForKey:@"text"]];
    }else{
        mwCell.companyName.text=[NSString stringWithFormat:@"%@",[[tempDic valueForKey:@"symbolname"] valueForKey:@"text"]];
    }

    [mwCell.companyName setTextColor:t.formfgColor];
    [mwCell.companyName setShadowColor:t.formShadowColor];

        float br_preVal=[[[prevDict valueForKey:@"bestbuyprice"] valueForKey:@"text"] floatValue];
        float br_nxtVal=[[[tempDic valueForKey:@"bestbuyprice"] valueForKey:@"text"] floatValue];

        float sr_preVal=[[[prevDict valueForKey:@"bestsellprice"] valueForKey:@"text"] floatValue];
        float sr_nxtVal=[[[tempDic valueForKey:@"bestsellprice"] valueForKey:@"text"] floatValue];


        if (br_nxtVal>br_preVal) {
            [mwCell.buyRate setBackgroundColor:t.socketHighbgColor];
            mwCell.buyRate.textColor=t.socketHighfgColor;
        }

        if (br_nxtVal<br_preVal){
            [mwCell.buyRate setBackgroundColor:t.socketLowbgColor];
            mwCell.buyRate.textColor=t.socketLowfgColor;
        }

        if (sr_nxtVal>sr_preVal) {
            [mwCell.sellRate setBackgroundColor:t.socketHighbgColor];
            [mwCell.sellRate setTextColor:t.socketHighfgColor];
        }

        if (sr_nxtVal<sr_preVal) {
            [mwCell.sellRate setBackgroundColor:t.socketLowbgColor];
            [mwCell.sellRate setTextColor:t.socketLowfgColor];
        }

    NSLog(@"tag of cell at index %i is %i",indexPath.row,mwCell.tag);

    mwCell.buyRate.textAlignment=NSTextAlignmentCenter;
    mwCell.sellRate.textAlignment=NSTextAlignmentCenter;
    mwCell.buyRate.text=[[tempDic valueForKey:@"bestbuyprice"] valueForKey:@"text"];
    mwCell.sellRate.text=[[tempDic valueForKey:@"bestsellprice"] valueForKey:@"text"];

    [mwCell.buyRate setShadowOffset:CGSizeMake(0, 0)];
    [mwCell.sellRate setShadowOffset:CGSizeMake(0, 0)];

    if ([mwCell.buyRate.text floatValue]<0) {
        [mwCell.buyRate setBackgroundColor:t.socketLowbgColor];
        [mwCell.buyRate setTextColor:t.socketLowfgColor];
    }

    if ([mwCell.sellRate.text floatValue]<0) {
        [mwCell.sellRate setBackgroundColor:t.socketLowbgColor];
        [mwCell.sellRate setTextColor:t.socketLowfgColor];
    }

    return mwCell;
}

MWCell h file

@interface MWCell : UITableViewCell

@property(nonatomic,strong)IBOutlet UILabel *companyName,*buyRate,*sellRate;
@property(nonatomic,strong)NSString *prev_br,*pre_sr;
@end

MWCell m file

#import "MWCell.h"

@implementation MWCell
@synthesize companyName,buyRate,sellRate;
@synthesize pre_sr,prev_br;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        pre_sr=@"";
        prev_br=@"";
        // Initialization code
    }
    return self;
}

@end

It is working fine , but facing problem while scrolling. If I scroll tableView its color automatically changes (even if no data coming from socket).I know every time we scroll tableView cell prepares and that causes custom cell's label color to change.

Is there any solution for this.

my screen shot is given below:----

enter image description here

Thanks!.

7
  • Can you explain what you are doing within if (mwCell==nil) { }
    – Mohith
    Jun 11, 2013 at 8:13
  • init UITableViewCell with nib if cell==nil Jun 11, 2013 at 8:33
  • Check where all are you setting isManipulated
    – Mohith
    Jun 11, 2013 at 8:36
  • remove it , it doesn't matter. Jun 11, 2013 at 9:02
  • Try to set the text before you compare with the previous value and also remove the last sets of if conditions.
    – Mohith
    Jun 11, 2013 at 9:09

1 Answer 1

0

Apply an alternative to set previous color:--

take two NSMutableArray and init with white colors.

NSMutableArray *celllc,*cellrc;

            prevScriptArray=[NSArray arrayWithArray:watchScriptArray];

            [celllc removeAllObjects];
            [cellrc removeAllObjects];

            for (int i=0;i<[watchScriptArray count];i++) {
                [cellrc addObject:[UIColor whiteColor]];
                [celllc addObject:[UIColor whiteColor]];
            }

Every time create cell by setting mwCell object to nil before checking it if(mwcell==nil) and set color to cell from array of color as given below in cellforRowAtIndexPath method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];

    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
    }

    MWCell *mwCell=(MWCell*)[tableView dequeueReusableCellWithIdentifier:@"mwCellIdentifier"];

    if (mwCell!=nil)mwCell=nil;

    if (mwCell==nil) {

        NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"MWCell" owner:self options:nil];

        for (id temp in topLevelObjects) {
            if ([temp isKindOfClass:[MWCell class]]) {

                mwCell=(MWCell*)temp;

                mwCell.buyRate.backgroundColor=[celllc objectAtIndex:indexPath.row];
                mwCell.sellRate.backgroundColor=[cellrc objectAtIndex:indexPath.row];

                mwCell.buyRate.textColor=([[celllc objectAtIndex:indexPath.row] isEqual:t.socketLowbgColor])?t.socketLowfgColor:t.socketHighfgColor;
                mwCell.sellRate.textColor=([[cellrc objectAtIndex:indexPath.row] isEqual:t.socketLowbgColor])?t.socketLowfgColor:t.socketHighfgColor;
            }
        }
    }

    NSDictionary *tempDic=[watchScriptArray objectAtIndex:indexPath.row];
    NSDictionary *prevDict=[prevScriptArray objectAtIndex:indexPath.row];

    if ([[tempDic valueForKey:@"expirydate"] valueForKey:@"text"]) {
        mwCell.companyName.text=[NSString stringWithFormat:@"%@-%@",[[tempDic valueForKey:@"symbolname"] valueForKey:@"text"],[[tempDic valueForKey:@"expirydate"] valueForKey:@"text"]];
    }else{
        mwCell.companyName.text=[NSString stringWithFormat:@"%@",[[tempDic valueForKey:@"symbolname"] valueForKey:@"text"]];
    }

    [mwCell.companyName setTextColor:t.formfgColor];
    [mwCell.companyName setShadowColor:t.formShadowColor];

        float br_preVal=[[[prevDict valueForKey:@"bestbuyprice"] valueForKey:@"text"] floatValue];
        float br_nxtVal=[[[tempDic valueForKey:@"bestbuyprice"] valueForKey:@"text"] floatValue];

        float sr_preVal=[[[prevDict valueForKey:@"bestsellprice"] valueForKey:@"text"] floatValue];
        float sr_nxtVal=[[[tempDic valueForKey:@"bestsellprice"] valueForKey:@"text"] floatValue];


        if (br_nxtVal>br_preVal) {
            [mwCell.buyRate setBackgroundColor:t.socketHighbgColor];
            mwCell.buyRate.textColor=t.socketHighfgColor;

            [celllc replaceObjectAtIndex:indexPath.row withObject:t.socketHighbgColor];
        }

        if (br_nxtVal<br_preVal){
            [mwCell.buyRate setBackgroundColor:t.socketLowbgColor];
            mwCell.buyRate.textColor=t.socketLowfgColor;

            [celllc replaceObjectAtIndex:indexPath.row withObject:t.socketLowbgColor];
        }

        if (sr_nxtVal>sr_preVal) {
            [mwCell.sellRate setBackgroundColor:t.socketHighbgColor];
            [mwCell.sellRate setTextColor:t.socketHighfgColor];

            [cellrc replaceObjectAtIndex:indexPath.row withObject:t.socketHighbgColor];
        }

        if (sr_nxtVal<sr_preVal) {
            [mwCell.sellRate setBackgroundColor:t.socketLowbgColor];
            [mwCell.sellRate setTextColor:t.socketLowfgColor];

            [cellrc replaceObjectAtIndex:indexPath.row withObject:t.socketLowbgColor];
        }

    NSLog(@"tag of cell at index %i is %i",indexPath.row,mwCell.tag);

    mwCell.buyRate.textAlignment=NSTextAlignmentCenter;
    mwCell.sellRate.textAlignment=NSTextAlignmentCenter;
    mwCell.buyRate.text=[[tempDic valueForKey:@"bestbuyprice"] valueForKey:@"text"];
    mwCell.sellRate.text=[[tempDic valueForKey:@"bestsellprice"] valueForKey:@"text"];

    [mwCell.buyRate setShadowOffset:CGSizeMake(0, 0)];
    [mwCell.sellRate setShadowOffset:CGSizeMake(0, 0)];

    if ([mwCell.buyRate.text floatValue]<0) {
        [mwCell.buyRate setBackgroundColor:t.socketLowbgColor];
        [mwCell.buyRate setTextColor:t.socketLowfgColor];
    }

    if ([mwCell.sellRate.text floatValue]<0) {
        [mwCell.sellRate setBackgroundColor:t.socketLowbgColor];
        [mwCell.sellRate setTextColor:t.socketLowfgColor];
    }

    return mwCell;
}

Better answer will be appreciated.If any?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.