0

我目前正在开发一个基于套接字的应用程序,在该应用程序中,我必须根据将标签的先前值与从套接字接收的当前值进行比较来更改自定义 UITableView 单元格内的 UIlabel 的颜色。

我还在自定义单元格中使用两个字符串变量来检查以前的值和当前值。这是索引路径代码处的 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

0

应用替代设置以前的颜色:-

取两个 NSMutableArray 并用白色初始化。

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

每次通过将 mwCell 对象设置为 nil 来创建单元格,然后再检查它 if(mwcell==nil) 并将颜色设置为来自颜色数组的单元格,如下在 cellforRowAtIndexPath 方法中给出

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

更好的答案将不胜感激。如果有的话?

于 2013-06-12T07:39:02.427 回答