Clarence
Clarence

Reputation: 1943

UITableview cell with alternating background image

I have a problem using the following code to set background of UITAbleview. Can I have a alternative images on the cell inside the UITable by using the following code?

e.g. cell 1 using X.png and cell 2 using Y.png?

tableView.opaque = NO; [tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"wood.png"]]]; cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lightwood.jpg"]];

Thanks a lot.

Upvotes: 0

Views: 438

Answers (1)

Emon
Emon

Reputation: 958

At first take a view in your .h file UIView *newView; declare its property and synhesize it at your .m file

Now in cellForRowAtIndexPath

if(cell==nil){
................
.........
newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
    [cell.contentView addSubview:newView];
................
...............
}
if(indexPath.row == 0){

    newView.backgroundColor = [UIColor [UIColor colorWithPatternImage:[UIImage imageNamed:@"lightwood.jpg"]];

}
if(indexPath.row == 1){

    newView.backgroundColor = [UIColor [UIColor colorWithPatternImage:[UIImage imageNamed:@"lightwood.jpg"]];

}
if(indexPath.row == 2){

    newView.backgroundColor = [UIColor [UIColor colorWithPatternImage:[UIImage imageNamed:@"lightwood.jpg"]];

}

If any further question please knock me.

EDIT:

if any problem still now then check this link http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

Upvotes: 1

Related Questions