当当前日期== Facebook生日列出日期时,我正在尝试更改单元格中标签的 text color
。
在cellForRowAtIndexPath:
id中执行此操作
if([curdatemonthstring isEqualToString:[[totalbirthlistArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]])
{
[nameLabel setTextColor:[UIColor purpleColor]];
}
这可以改变颜色但是当我滚动它时会改变回原来的颜色。我该如何解决这个问题?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableidentifier=@"SimpleTableItem";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableidentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableidentifier]autorelease];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
contentview = [[[UIView alloc]init] autorelease];
[contentview setFrame:CGRectMake(0, 0,320,80)];
//Display Friends Name
nameLabel = [[[UILabel alloc] init]autorelease];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[nameLabel setFrame:CGRectMake(76, 5, 200, 45)];
[nameLabel setFont:[UIFont fontWithName:@"Politica" size:20]];
[nameLabel setText:[[[monthdataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]objectForKey:@"name"]];
if([curdatemonthstring isEqualToString:[[totalbirthlistArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]])
{
[nameLabel setTextColor:[UIColor purpleColor]];
}
[contentview addSubview:nameLabel];
[cell.contentView addSubview:contentview];
return cell;
[birthdaylist reloadData];
}
答案 0 :(得分:0)
因为tableview在滚动时重用它的单元格,所以问题主要是因为你在if(!cell)
条件内编写了上面的逻辑。
答案 1 :(得分:0)
要排除数据的潜在问题,请尝试替换
if([curdatemonthstring isEqualToString:[[totalbirthlistArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]])
与
if(indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 15)
您是否始终看到第0,1和15行的颜色设置正确? 可能是totalbirthlistArray在滚动之间无意中发生了变化,比如在[birthdaylist reloadData]调用中吗?
答案 2 :(得分:0)
用这个替换你的cellForRowAtIndexPath。我想,这将解决你的问题。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableidentifier=@"SimpleTableItem";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableidentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableidentifier]autorelease];
contentview = [[[UIView alloc]init] autorelease];
[contentview setFrame:CGRectMake(0, 0,320,80)];
//Display Friends Name
nameLabel = [[[UILabel alloc] init]autorelease];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[nameLabel setFrame:CGRectMake(76, 5, 200, 45)];
nameLabel.tag = 1;
[nameLabel setFont:[UIFont fontWithName:@"Politica" size:20]];
[nameLabel setText:[[[monthdataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]objectForKey:@"name"]];
[contentview addSubview:nameLabel];
[cell.contentView addSubview:contentview];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UILabel *nameLabel = [cell.contentView viewWihtTag:1];
if([curdatemonthstring isEqualToString:[[totalbirthlistArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]])
{
[nameLabel setTextColor:[UIColor purpleColor]];
}
return cell;
[birthdaylist reloadData]; //I don't what is the use of this.
}