UITabBarController moreNavigationController - cell background image of the cell

Wow, this was sometime a sip. :)

We've already seen a nice flow on customizing a larger menu (table view) in the tab bar .

For our next trick ... how can we add a background image to table cells?

I thought I could get away with overriding tableView: cellForRowAtIndexPath: in my Data Table View class (see previous link for methodology). Alas, this only changes the background under the images and accessories to the left and right of each cell.

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

    // Let the original Data Source get a hold of the cell first.
    UITableViewCell *cell = [originalDataSource tableView:tableView cellForRowAtIndexPath:indexPath];

    // If we override the textColor, that works fine. (Commented out for now.)
    //cell.textLabel.textColor = [UIColor whiteColor];

    // If we override the background view … that doesn't work so well.
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBackground.png"]];

    return cell;
}

      

I know imageNamed: is evil and maybe I need to create one background image and just reuse it all over (versus allocating multiple UIImageView objects ). Besides these subjects, any thoughts? (I've tried setting the textLabel's backgroundColor too, to no avail ... unless I'm doing something wrong.)

+2


source to share


2 answers


Here is the answer ...

Add the code to



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

MoreTableViewDataSource method

+2


source


In addition to posting Sijo's answer (thanks!), I just realized there was another thread on SO that takes a similar approach ... except that I understand this code a little more. However, I have never cross-referenced this question / thread! So this is it. See, in particular, Ian1971's answer.



+1


source







All Articles