2011-06-26 105 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)