开发者

UITableViewCell customization - change background

I'm sure this is a question that been asked and answered, but I can't seem to find what I need. I am trying to customize the background of my UITableView, so to do this, I created a subclass of UITableViewCell, and built my custom cell with the IB. I successfully connected the custom cell to the table view, so the cell I get is getting displayed. I am trying to change the background of each cell, so here is what I use:

if(row == 1){
    rowBackground = [UIImage image开发者_JAVA技巧Named:@"VehicleExpenses_button.png"];
    selectionBackground = [UIImage imageNamed:@"VehicleExpenses_button_selected.png"];
    ((UIImageView *)cell.backgroundView).image = rowBackground;
  //  ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
}

within the

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

method of my Controller class. I checked with the debugger, and all the lines in the code are getting called...

Any ideas?

This is the entire method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"HomeViewCell";
    static NSString *CellNib = @"HomeViewCell";

    UIImage *rowBackground;
    UIImage *selectionBackground;

    NSInteger row = [indexPath row];


    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
    }

    if(row == 1){

        rowBackground = [UIImage imageNamed:@"VehicleExpenses_button.png"];
        selectionBackground = [UIImage imageNamed:@"VehicleExpenses_button_selected.png"];
        ((UIImageView *)cell.backgroundView).image = rowBackground;
      //  ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
    }

    // perform additional custom work...

    return cell;
}


Maybe I'm wrong but I don't see where you initialized the backgroundView as a UIImageView. Here is what I would add to the lines where you create the cells:

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
        cell.backgroundView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, cellHeight)] autorelease];
        cell.selectedBackgroundView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, cellHeight)] autorelease];
    }

Note: replace "cellHeight" with your desired height.

I hope this helps.


You can use this:

UIImageView *cellBackGround = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selectedRow.png"]];
[[tableView cellForRowAtIndexPath:indexPath] setBackgroundView:cellBackGround];
[cellBackGround release];


Small problem. Instead of casting and setting the backgroundView and selectedBackgroundView's image property, set them to the UIImageView created from your images.

cell.backgroundView = [[[UIImageView alloc] initWithImage:rowBackground] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:selectionBackground] autorelease];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜