0

当当前日期 == 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];




}
4

3 回答 3

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.

}

于 2013-08-23T06:22:12.013 回答
0

要排除数据的潜在问题,请尝试替换

if([curdatemonthstring isEqualToString:[[totalbirthlistArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]])

if(indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 15)

您是否总是看到第 0 行、第 1 行和第 15 行的颜色设置正确?会不会是在 [birthdaylist reloadData] 调用中,在滚动之间无意地改变了 totalbirthlistArray?

于 2013-08-22T12:41:47.897 回答
0

因为 tableview 在滚动时重用了它的单元格,所以问题主要是因为你在内部if(!cell)条件下编写了上面的逻辑。

于 2013-08-22T12:00:47.587 回答