2013-08-22 66 views
0

我試圖更改標籤的text color在單元格時當前日期== Facebook的生日名單日期。更改的UILabel的文本顏色中的UITableViewCell

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 *[email protected]"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

請提供完整的代碼。 –

+1

我認爲您已經將它寫入了if(cell == nil)的條件 – SRI

+0

明確顯示您在創建單元格的位置,重置該單元格的顏色很可能是因爲未正確創建單元格。 –

回答

0

因爲tableview中重用它在滾動細胞,問題大多是因爲你寫上面的邏輯中if(!cell)條件。

+0

不能改變顏色。 – rajesh

+0

您是否重設其他地方的文字顏色? – iPP

+0

no.I此時只改變顏色。 – rajesh

0

要排除與數據的潛在問題嘗試用

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

更換

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

你總是看到行0,1的顏色和15套正常嗎? 難道是因爲totalbirthlistArray在滾動條之間無意中發生了變化,比如在[birthdaylist reloadData]中調用?

+0

我刪除了[birthdayList reloaddata]。但是,沒有改變。 – rajesh

+0

正如Rheven指出的[birthdaylist reloadData]在這個函數中永遠不會被調用,並且沒有任何作用(也許你在其他地方稱它爲?)。你有沒有嘗試用硬編碼替換if([curdatemonthstring isEqualToString ...])條件,就像我的例子中那樣? – dmitri

+0

是的,但是indexpath.row == 0,1,15的所有文本都變成了紫色。 – rajesh

0

替換您的cellForRowAtIndexPath與this.I認爲,這將解決您的問題。

- (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *[email protected]"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. 

}

+0

對不起,我刪除了reloaddata。 – rajesh

+0

您是否獲得了所需的輸出? – Bharath