UITableView / Cell Custom Draw

Discussion in 'iOS Programming' started by elronaldo, Sep 22, 2008.

  1. elronaldo macrumors newbie

    Joined:
    Sep 22, 2008
    #1
    I have setup a Plain UITableView with a backgroundColor of UIColor clearColor. The trouble is that all the cells are now draw as transparent. I am able to resolve the background color of the cells that are draw from my data source.

    Could someone please let me know if I can draw a white background on the cells that have nothing assigned? For example if I have only one cell added to the data source then all the remaining cells are transparent. I was hoping that I could use one of the following methods to custom draw the background on the other cells that are not assigned by my data source:

    – rectForSection:
    – rectForRowAtIndexPath:

    Is this possible? Thanks!
     
  2. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #2
    Don't know if its the best way to do it but I'd do it this way:-

    create a UIView with a white background:-

    Code:
    UIView * emptyCellBG = [[UIView alloc] initWithFrame:CGRectZero];
    [emptyCellBG setBackgroundColor:[UIColor whiteColor]];
    then add the view to the empty cells:-

    Code:
    [<cellname> addSubview:emptyCellBG];
    Replace <cellname> with the name of your cell.

    I'm not sure if the size of the UIView will be correct seeing as I'm using CGRectZero but surely you can figure that one out on your own if its not right.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    It is your cellForRowAtIndexPath: callback that creates all the cells. You need to set the background color appropriately there. You need to understand that the tableview can call you back multiple times for the same row. If you create a row and then that row scrolls offscreen and back onscreen it will ask you to create that cell again. You need to set the state correctly in cellForRowAtIndexPath:
     
  4. elronaldo thread starter macrumors newbie

    Joined:
    Sep 22, 2008
    #4
    Where would you recommend adding that?

    From what I have tested I do not believe that I can apply any customization to the cells that are not in the data source. That is why I thought I would custom draw the UITableView.

    When I add dummy cells to the table view they seem to interfere with moving the cells.
     
  5. elronaldo thread starter macrumors newbie

    Joined:
    Sep 22, 2008
    #5
    Yes, I understand that. However cells that are not in my data source are not triggered through the cellForRowAtIndexPath: callback. For example if I have one object in my data source it will call the cellForRowAtIndexPath for only that cell. I am able to set this row to my specific backgroundColor. However all remaining cell are shown as transparent.

    Does anyone have any examples for custom drawing using, rectForSection: or rectForRowAtIndexPath: ? I believe that these may be able to help with my issue however I can't find any samples that custom draw using these methods.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    What do you mean by:

    You tell the table how many rows there are. There are no extra rows unless you tell it that there are.

    I guess maybe you mean empty rows not empty cells. Those rows with nothing in them don't have cells in them unless you create those cells. There's no way you can affect the drawing of the empty rows individually because there are no cells there.

    Why don't you set the background color of your cells and not of the entire table?
     
  7. elronaldo thread starter macrumors newbie

    Joined:
    Sep 22, 2008
    #7
    Sorry I did mean empty rows. I am setting the background color of my cells manually to white. The reason I am setting the background color of the UITableView to clear is so that it can show an UIImageView underneath.

    The trouble is that the empty rows are showing as transparent. Is it not possible to have the UITableView background as transparent and then have all cells and empty rows drawn with a white background. I was hoping that I could somehow manually redraw the empty rows background by custom drawing these. I am unsure how I would do this though.
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    Not really. The only thing I can think of is to actually add empty cells to the table. The problem is though that no matter how many empty cells you add the user can always scroll down to the end. You might try that and see if it comes close to the effect you want. Just add enough rows to fill the height of the screen.

    I guess what you want is one or two rows with content and the rest of the height of the screen is rows with white background over the image. I guess the user is unlikely to scroll down more if the rows at the bottom of the screen look empty.
     
  9. elronaldo thread starter macrumors newbie

    Joined:
    Sep 22, 2008

Share This Page