2012-10-25 73 views
0

我想知道我怎樣才能把圖片上的代碼即時通訊使用的自定義單元格在自定義TableViewCell設置圖像

的背景是這樣的,但這種設置所有表相同的圖像 我希望每個細胞都有自己的背景,具體取決於誰是消息所有者 我希望你能幫助我,這就是殺了我!

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

    UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:@"ChatListItem"]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatListItem" owner:self options:nil]; 
     cell = (UITableViewCell *)[nib objectAtIndex:0]; 
    } 

    NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row]; 
    UILabel *textLabel = (UILabel *)[cell viewWithTag:1]; 
    textLabel.text = [itemAtIndex objectForKey:@"text"]; 
    UILabel *userLabel = (UILabel *)[cell viewWithTag:2]; 
    userLabel.text = [itemAtIndex objectForKey:@"user"]; 

    if(usernameText=userLabel){ 
     UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"[email protected]"]]; 
     [cell setBackgroundView:img]; 
     [img release];   
    } 
    else{ 
     UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"[email protected]"]]; 
     [cell setBackgroundView:img]; 
     [img release]; 

    } 

    return cell; 
} 
+0

請不要添加簽名 – user427969

回答

3

這條線不看對我太好了:

if (usernameText=userLabel) 

也許應該是:

if ([usernameText isEqualToString:userLabel.text]) 
+0

幾乎總是,無論何時您需要評估NSStrings,請使用isEqualToString:如@EarlyRiser – petershine

+0

所述,感謝它的完美工作:) – slozano95

0

試試這個,

tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"[email protected]"]]; 

或者,

cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@「[email protected]」]];