Table cell accessory view

Discussion in 'iOS Programming' started by Tiiba, Jul 14, 2010.

  1. macrumors newbie

    Joined:
    Jul 9, 2010
    #1
    I have a table, and I want each cell to contain, besides a bit of text (textLabel) and a picture (imageView), also two custom buttons that would respod to clicks. But they won't show up.

    What I did was create a subclass of UIView, to which I added the buttons. I gave the buttons text to display. I called sizeToFit on each button, and then on the container. Then, I added the new view to the cell as accessoryView. I ran the program, and saw that my effort was for naught.

    The view I want to see:
    PHP:
    -(idinit
    {
        
    button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        
    button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        
        [
    button1 sizeToFit];[button2 sizeToFit];[self sizeToFit];
        
        
    item i;
        
    send.titleLabel.text = @"1";
        
    feedback.titleLabel.text = @"2";
        
        
        [
    self addSubview:button1];
        [
    self addSubview:button2];
        
        return 
    self;
    }
    The cell:

    PHP:
    - (UITableViewCell* )tableView:(UITableView* )tableView cellForRowAtIndexPath:(NSIndexPath* )indexPath
    {
        
    UITableViewCellcell = [tableView dequeueReusableCellWithIdentifier:@"123"];
        if (
    cell == nil)
        {
            
    cell = [[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"123"];

        }    
        
    cell.accessoryView = [[ItemCellAccessory allocinit];
        [
    cell.accessoryView retain];
        return 
    cell;
    }
     
  2. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    Take a look at Apple's TableViewSuite sample code for ways to implement custom cells.
     
  3. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    First, let's start by looking at problems with the code you supplied, Tiiba.
    Where and how are send and feedback defined?

    Also, what is the purpose of this line?:
    Code:
    item = i; 
     
  4. thread starter macrumors newbie

    Joined:
    Jul 9, 2010
    #4
    Ah, sorry. I changed the code from the original to make it more generic-looking (which might not have served any purpose, now that I think of it), and forgot a few things. send and feedback are button1 and button2
     
  5. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    init methods must call super. UIView subclasses should override initWithFrame: (and call super initWithFrame:).
     
  6. thread starter macrumors newbie

    Joined:
    Jul 9, 2010
    #6
    Okay, it turns out you have to add things to the cell's contentView. Fine. But I have another question: when can you find out the table's width? I keep finding out that it's null, no matter what I try. The same thing happens when I try to find out the size of the window.
     
  7. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Your UIButton needs its frame set. Otherwise, it is defaulted to CGRectZero, which isn't going to show anything.
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    No, you can set the accessoryView, like you were doing before you edited your post. You just need to make sure the view you're setting has its frame set.
     
  9. thread starter macrumors newbie

    Joined:
    Jul 9, 2010
    #9
    PHP:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
    UITableViewCellcell = [tableView dequeueReusableCellWithIdentifier:@"123"];
        
    UIButtonbt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        
    bt.titleLabel.text = @"1";
        
    bt.titleLabel.frame CGRectMake(0050,50);
        
    bt.frame CGRectMake(0050,50);
        
        if (
    cell == nil)
        {
            
    cell = [[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"123"];
        }    
        [
    cell.accessoryView addSubview:bt];
        return 
    cell;
    }
    This doesn't work.

    And in the contnt view, I can see the button, but not what's in it.
     
  10. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Setting the accessoryView and adding a subview to it are two different things, especially if you consider what the value of accessoryView is before calling addSubview: on it.

    Think about the frame for contentView, as well as its relation to textLabel.
     
  11. thread starter macrumors newbie

    Joined:
    Jul 9, 2010
    #11
    Ah. Thanks.

    Don't get that one. I was talking about the button. Anyway, I made it the accessory view now, but it still has no text in it.
     
  12. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #12
    This thread baffles me. The topic of table views is one of the most documented and blogged about and sample coded topics in the known universe.

    OP, read the docs, look at the blogs and sample code. The answer is there.

    Look at Apple's AccessoryView sample code, if you must do this with an accessory.
     
  13. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    That's because UIButton's textLabel property is read-only. You should look at the instance methods to determine how to set it.
     
  14. thread starter macrumors newbie

    Joined:
    Jul 9, 2010
    #14
    I didn't try to replace the text label. I set its text value.

    And the docs make no mention of textLabel, only titleLabel. Is that what you mean?
     
  15. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    So, you think you should be able to change the property of an object that is read-only?

    Oops, sorry. Yeah, that's the one. My bad.
     
  16. thread starter macrumors newbie

    Joined:
    Jul 9, 2010
    #16
    Far be it from me. But UILabel.text is read-write. I confirmed this.

    Or are you saying that if a read-only property points to an object, THAT obect's properties are fixed?
     
  17. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #17
    I thought that's what I was trying to say (sounds logical doesn't it?) but seems Apple is trying to mess with me. So, for a UIButton, those properties of the titleLabel that have corresponding instance methods must use those instance methods and not access the properties directly. See setTitle:forState:, setTitleColor:forState:, etc.
     
  18. thread starter macrumors newbie

    Joined:
    Jul 9, 2010
    #18
    Well, no, not with my background. In Java, you can use a final method to get an instance of a final class, assign it to a final field, and then modify any of its non-final fields (if they're visible in this scope) all day long. Same in C#, plus readonly properties, unless I'm mistaken.

    For the record, I figured out what you were referring to. Thanks.

    (Do you have an answer to the other question, regarding objects' sizes?)
     
  19. tutiplain, Mar 16, 2011
    Last edited: Mar 16, 2011

    macrumors member

    Joined:
    Feb 4, 2011

Share This Page