0
votes

I'm creating an ios app where I want to use a switch as the accessory for a tableview. I've found the following sample of how to use a UISwitch as the accessory: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/ManageSelections/ManageSelections.html (see "Responding to Selections" section).

What I'd like to do is to use a DCRoundSwitch (https://github.com/domesticcatsoftware/DCRoundSwitch) instead of a UISwitch so I can customize the text. But when I create the accessory in the way that is suggested by the sample code, I just get the round knob, I don't get the whole switch. The code I'm using to add the accessory is:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"PersonCell";

    MyPerson *person = [self.dataController objectInListAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    DCRoundSwitch *switchObj = [[DCRoundSwitch alloc] initWithFrame:CGRectMake(1.0, 1.0, 20.0, 20.0)];
    switchObj.onText = @"in";
    switchObj.offText = @"out";

    [switchObj addTarget:self action:@selector(personIn:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
    cell.accessoryView = switchObj;

    [[cell textLabel] setText:person.name];
    [[cell detailTextLabel] setText:person.label];
    return cell;
}

What can I do to make the DCRoundSwitch work as a TableView accessory?

Thanks!

1
where is the code that actually assigns the switch to the accessory view? As in cell.accessoryView = switchObj;charleyh
ack, copy/paste error. Updated code to include that. Hopefully it's consistent - I've been playing with the code.nojo

1 Answers

0
votes

I think you width is just to narrow -- try something like 60 or 70 for the width (the default width for a UISwitch in the storyboard is 79).