我目前正在开发一个基于套接字的应用程序,我必须在自定义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 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
工作正常,但在滚动时遇到问题。 如果我滚动tableView它的颜色会自动更改(即使没有来自套接字的数据)。我知道每次滚动tableView单元格准备并导致自定义单元格的标签颜色发生变化。
有没有解决方案。
我的屏幕截图如下:----
谢谢!
答案 0 :(得分:0)
应用替代方法来设置上一个颜色: -
使用白色的两个NSMutableArray和init。
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来创建单元格(mwcell == nil) 并将颜色从colorforRowAtIndexPath方法
中给出的颜色数组设置为单元格- (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;
}
更好的答案将不胜感激。如果有的话