0

I have a TableView that has UISwitches in it. The switches are slightly off the screen. how do I make the display correctly.

Switch

Here is my code that displays the switches.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"POICell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if(!cell){

    //set the cell text to the
    cell.textLabel.text = [self.catNames objectAtIndex:[indexPath row]];
    NSString *toggle = [self.toggleArray objectAtIndex:[indexPath row]];
    //add switch
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
    cell.accessoryView = switchView;

    if ([toggle isEqualToString: @"OFF"]) {
        [switchView setOn:NO animated:NO];
    }else{
        [switchView setOn:YES animated:NO];
    }


    [switchView addTarget:self action:@selector(switchChanged: ) forControlEvents:UIControlEventValueChanged];
    // Configure the cell...

    }
    return cell;
}
Will
  • 3,004
  • 29
  • 43
  • Not related to your question: use NSString's `isEqualToString:` instead of `==`. You probably want to match the string content instead of checking pointer equality. – Rengers Nov 01 '12 at 16:55
  • Oh yeah someone pointed that out to me the other day :P – Will Nov 01 '12 at 16:58
  • is the `if (!cell)` statement missing on purpose or are you using static cells or something? also, is the width of your cell "standard"? meaning, did you change it somewhere? – jere Nov 01 '12 at 16:59
  • No I havent set the cell size anywhere – Will Nov 01 '12 at 16:59
  • There is also a memory leak (not with ARC, but still...) when you create `*toggle`. Merge those two lines into one: `NSString *toggle = [self.toggleArray objectAtIndex:[indexPath row]];` – Rengers Nov 01 '12 at 17:02
  • Thanks guys. im still new to objective-c. – Will Nov 01 '12 at 17:04
  • If I had to guess, I would say that it is related to using `CGRectZero` for the frame of the switch.... – lnafziger Nov 01 '12 at 17:05

2 Answers2

1

Rather than adding a UISwitch to the standard cell, I'd create a custom UITableViewCell that contained the text label and the switch.

But after looking at the Apple docs, it seems what you want to achieve is a valid approach.

Table View Programming Guide for iOS

And your code already looks a lot like this SO question: How to create a UITableViewCell with a UISwitch and get the data? So I am not sure why the UISwitch isn't displaying as expected.

Community
  • 1
  • 1
Peter M
  • 7,309
  • 3
  • 50
  • 91
-1

I think the position of the default accessoryView can't be moved without a problem, as the TableViewCell will reposizion it in many situations.

So adding it as a subview might be the better idea, instead of trying to "hack" the default behavior.

Thyraz
  • 2,412
  • 2
  • 21
  • 23
  • actually, adding it as subview of the cell is a bad idea. accesory views were meant for this sort of things – jere Nov 01 '12 at 17:00