2013-08-22 3 views
0

Я пытаюсь изменить text color метки в клетке, когда Current Date == Facebook День рождения Список дат.Изменение цвета текста UILabel в UITableViewCell

В cellForRowAtIndexPath: ид сделал это

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

Нет. Я меняю цвет только на этот раз. – rajesh

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]?

+0

Я удаляю это [birthdayList reloaddata] .but, никаких изменений. – rajesh

+0

Как заметил Ревен [имя_пользователя reloadData], никогда не вызывается в этой функции и не имеет никакого эффекта (может быть, вы называете это в другом месте?). Вы пытались заменить if ([curdatemonthstring isEqualToString ...]) условие с жестко закодированным, как в моем примере? – dmitri

+0

Да, но весь текст indexpath.row == 0,1,15 изменен на фиолетовый цвет. – rajesh

0

замените ваш cellForRowAtIndexPath этим. Я думаю, это решит вашу проблему.

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

Смежные вопросы