0

現在、ソケットから受け取ったラベルの以前の値と現在の値を比較することに基づいて、カスタム UITableView セル内の UIlabel の色を変更する必要があるソケットベースのアプリケーションに取り組んでいます。

カスタムセルでも2つの文字列変数を使用して、以前の値と現在の値を確認します。インデックス パス コードの cellFor 行は次のとおりです...

- (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;
}

セル内のテキストで現在の配列をチェックするのではなく、現在の配列値で以前の配列値をチェックするようにコードを変更しました。しかし、これも正しく機能していないようです。スクロールすると、他の行の色も塗りつぶされます。

- (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 ファイル

@interface MWCell : UITableViewCell

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

MWCell m ファイル

#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

正常に動作していますが、スクロール中に問題に直面しています。tableView をスクロールすると、その色が自動的に変わります (ソケットからデータが来ていなくても)。tableView セルの準備をスクロールするたびに、カスタム セルのラベルの色が変更されることがわかっています。

これに対する解決策はありますか。

私のスクリーンショットを以下に示します:----

ここに画像の説明を入力

ありがとう!。

4

1 に答える 1