Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a custom background on grouped UITableView cells?

I am trying to set a custom background for the rows of a grouped UITableView and I have absolutely no idea how to achieve that.

The table design I am trying to apply on my table looks like this:

Custom Designed Grouped UITableView

And I have sliced it into 3 cell types: top, middle and bottom. How can I apply the custom background for each of the table's cells?

like image 658
Itamar Avatar asked Mar 01 '12 08:03

Itamar


2 Answers

You can set UIImageView as cell backgroundView for first, middle and last cell in function:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell  forRowAtIndexPath:(NSIndexPath *)indexPath

Check good (and simple) example at: http://www.planet1107.net/blog/tips-tutorials/custom-grouped-cell-tutorial/

like image 143
Jakub Gert Avatar answered Sep 28 '22 11:09

Jakub Gert


In cellForRowAtIndexPath, check the type of current row and then assign the corresponding background to it.

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

    //  Create cell
    //  ...

    //  Configure cell

    switch (cellType)  {
       case TOP:
           cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top.png"]];  
           break;
       case MIDDLE:
           ...
    }
}
like image 36
Hanon Avatar answered Sep 28 '22 13:09

Hanon