2011-06-26 143 views
0

我嘗試這種情況,但不工作。如果senderlabel = key @「sender」對每一行使用green.png或grey.png。uitableview問題

if (senderLabel.text = [tempMsg objectForKey:@"sender"]) 
    { 
     [cell.msgText setText:[tempMsg objectForKey:@"message"]]; 

     [messTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

     //[cell setFrame:CGRectMake(0,0, size.width, size.height)]; 
     [cell.msgText setFrame:CGRectMake(15,3, size.width, result)]; 




     UIImage* balloon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15]; 
     UIImageView *newImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, size.width+10, result+10)]; 
     UIView *newView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)]; 

     [newImage setImage:balloon]; 
     [newView addSubview:newImage]; 
     [cell setBackgroundView:newView]; 

    }else { 
     [cell.msgText setText:[tempMsg objectForKey:@"message"]]; 

     [messTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

     //[cell setFrame:CGRectMake(0,0, size.width, size.height)]; 
     [cell.msgText setFrame:CGRectMake(15,3, size.width, result)]; //propriété du texte 


     UIImage* balloon = [[UIImage imageNamed:@"green.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15]; 
     UIImageView *newImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, size.width+10, result+10)]; 
     UIView *newView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)]; 

     [newImage setImage:balloon]; 
     [newView addSubview:newImage]; 
     [cell setBackgroundView:newView]; 

    } 



    return cell; 

} 

回答

0

if(senderLabel.text = [tempMsg objectForKey:@「sender」])不正確。您需要在比較中使用isEqual。

if ([senderLabel.text isEqual:[tempMsg objectForKey:@"sender"]]) 
0

首先,這裏

if (senderLabel.text = [tempMsg objectForKey:@"sender"]) 

你正在分配,而不是一個比較,比較是

if (senderLabel.text == [tempMsg objectForKey:@"sender"]) 

但是,如果它是完全相同的對象會比較在內存中,這可能不是你想要的,如果你想檢查字符串是否有相同的文本,你必須使用

[string isEqualToString:anotherString] 
+0

THX但我所有的數據用綠色。在每一行中,我有不同的數據 – XcodeMania

+0

我想我必須使用indexPath.row但是如何? – XcodeMania

+0

您可以繼承UITableViewCell並在其中添加自己的背景,它會更容易,更快速。請記住重寫setFrame:更改背景的大小。 – EmilioPelaez

0

要獲得indexpath你可以使用這樣的:

NSIndexPath *indexpath = [NSIndexPath indexPathForRow:0 inSection:0]; 

,並進行比較:

if ([string compare:anotherString] == NSOrderedSame)