slozano95
slozano95

Reputation: 278

Setting an Image on Custom TableViewCell

I want to know how can I put an image on the background of the custom cell

The code im using is this one, but this sets all the table to the same image I want each cell to have its own background depending on who is the message owner I hope you can helpme this is killing me !

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

Upvotes: 0

Views: 182

Answers (2)

Sudha Chandran B.C.
Sudha Chandran B.C.

Reputation: 82

Try this,

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

Or,

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

Upvotes: 0

EarlyRiser
EarlyRiser

Reputation: 736

This line doesn't look too good to me:

if (usernameText=userLabel)

should perhaps be:

if ([usernameText isEqualToString:userLabel.text])

Upvotes: 3

Related Questions