how should i be adding switches to custom table cells?

Discussion in 'iOS Programming' started by Chirone, Sep 17, 2009.

  1. Chirone macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #1
    you see in the settings that you can have a switch for turning on airport on and off in a table cell

    i figure it's a custom table cell because there's no option to add sliders or switches or segmented buttons to a table cell.

    however when i do this, if i hit the cell itself and not the switch it distorts and doesn't look the way it should be.

    what's the best way for adding these components to a table cell?
     
  2. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    custom table cell is the way. what's distorting? the entire cell? can you post images?

    also, have you read this book yet? it's a truely great book, really easy to follow and you'll learn a lot. def read it if you haven't.
     
  3. Chirone thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #3
    [​IMG]
    like that

    what i did to resolve this seems like a real hack...
    In the didSelectRowAtIndexPath method i put in some code that would switch the switch to the opposite of whatever it was when the cell was touched....
    like this:
    Code:
    	NSInteger section = [indexPath indexAtPosition: 0];
    	NSInteger itemIndex = [indexPath indexAtPosition: 1];
    	if((section == 0 || section == 1) && (itemIndex == 0 || itemIndex == 1 || itemIndex == 2)) {
    		[((UISwitch*)[tableView cellForRowAtIndexPath: indexPath].accessoryView) setOn: 
    		!((UISwitch*)[tableView cellForRowAtIndexPath: indexPath].accessoryView).on animated: YES];
    		[tableViewOutlet deselectRowAtIndexPath:[tableViewOutlet indexPathForSelectedRow] animated:NO];
    		return;
    	}
    
    it feels like a bad hack........
    the switch would only work when the actual button itself was hit, hitting the word "on" or "off" or the table cell would cause it to do what i showed in that picture above
    yeah you can't turn it on or off by hitting the word "on" or "off" but you can if you hit the button itself and the table cell.......
    seems like a lame trade off... either have your button screw itself over when you hit the table cell and have it work like it should when it's hit or have it not screw itself over and not work like it should.....
     
  4. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    oh right, accessory views... forgot about those. personally i prefer to roll my own cells to gain full control. i do remember i had a similar problem once with tapping the "on"/"off" text of a UISwitch, although my problem never distorted the actual UISwitch but allowed for undesirable touch locations to register. i forget how i solved it... i think i wrote something in the touch methods.

    [EDIT] if i understand your attempts correctly, you are trying to toggle the UISwitch simply by touching it's tableview cell? this could lead to the inability to properly scroll the table view without unintentionally activating some UISwitchs (without some additional code in the touch methods, of course) if the user holds down the cell long enough. it's certainly not the proper way to toggle a UISwitch inside a cell. check out the settings of your iPhone to see how those cells manage a UISwitch.
     
  5. Chirone thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #5
    it's weird though... that method works on custom table cells that didn't have their switches set by the accessory views.... :confused:
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    Try not adding the switch as an accessoryView. Just add it as a subview.

    There were reports of this bug on the Apple iPhone forum but I don't know if they were also related to using the switch as an accessoryView. FWIW, I have a cell with a switch in it as a subview and I've never seen this problem.

    BTW, this is more convenient:

    Code:
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
     
  7. Chirone thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #7
    ok, so i put in this code here:

    Code:
    [cell addSubview: [[[UISwitch alloc] initWithFrame: r] autorelease]];
    and it put a switch to the table cell but it still made it go funny when the table cell was clicked... :confused:

    and thanks for the tip about indexpath phoneyDeveloper :)
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    That should be

    Code:
    [cell.contentView addSubview: [[[UISwitch alloc] initWithFrame: r] autorelease]];
    But I'm not sure if that will make a difference. I have one switch in one table cell in my app but I've never seen this problem.
     
  9. Chirone thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #9
    may i ask how you built that part of your app?

    a resolution has been found...
    just have to change the selection style of the cell to UITableViewCellSelectionStyleNone
    ...
     
  10. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #10
    I think for no particular reason I built that cell with IB and load it. I have a UI that is a grouped table with two sections. Two rows in the first section and three rows in the second section. The two rows in the first section are unique. The three rows in the second section are the same kind of cell but the contents are different.

    All the rows except the row with the switch in it are created in code and they're basically label value cells.
     

Share This Page