0

I'm basically having the same issue as this question. But guess what? That question doesn't have any good answer.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if([indexPath row] == 0){

        UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];
        if( aCell == nil ) {
            aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SwitchCell"];
            aCell.textLabel.text = @"Colors";
            aCell.selectionStyle = UITableViewCellSelectionStyleNone;

            /*
            UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
            aCell.accessoryView = switchView;
            [switchView setOn:NO animated:NO];
            [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
            */
            UIStepper *stepperView = [[UIStepper alloc] initWithFrame:CGRectZero];
            NSLog(@"The stepper is %@", stepperView);
            aCell.accessoryView = stepperView;
           [stepperView setMaximumValue:10];
           [stepperView setMinimumValue:0];
           [stepperView addTarget:self action:@selector(stepperChanged) forControlEvents:UIControlEventValueChanged];
           NSLog(@"The stepper is %@", stepperView);
       }
       return aCell;
    }

    UITableViewCell *aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SwitchCell"];

    NSString *s = @"foo";
    s = [s stringByAppendingFormat:@"%d", [indexPath row]];
    aCell.textLabel.text = s;

return aCell;

}

So if I switch the code to the commented out code, that works just fine (displaying the UISwitch). So all I tried to do was replace the UISwitch with the UIStepper and it just doesn't show up. I hate objective-c.

Oh, and both of those printlines show the value is (null). Right after I create it, it's null?

EDIT: iOS 5.0. I feel like the answer might have to do with why this:

NSLog(@"%@", [[UISwitch alloc] init]);

is not null while

NSLog(@"%@", [[UIStepper alloc] init]);

is null.

Community
  • 1
  • 1
Andrew
  • 1,571
  • 17
  • 31
  • Did you try to simply alloc]init] the stepper? – danypata Jun 15 '13 at 21:41
  • Yep. Tried that. Even `NSLog(@"The stepper is %@", [[UIStepper alloc] init]);` says it's (null). It's probably something really stupid. – Andrew Jun 15 '13 at 21:50
  • 2
    What's your iOS target ? – danypata Jun 15 '13 at 21:57
  • This project is set up for 5.0. – Andrew Jun 15 '13 at 22:00
  • Well my friend this is one of the strangest issues that I ever encountered, maybe you can try the developer's "magic", xcode restart, simulator restart and app remove from device, also you should try to add a stepper to a simple view in your application and after that get back to us ;) – danypata Jun 15 '13 at 22:10
  • Ugh I'm dumb. I knew it was something stupid. The simulator was for an iPhone 4.3. But why in the world wouldn't that throw an error? I still hate objective-c. – Andrew Jun 15 '13 at 22:18
  • :)))) Yep, that's a dumb mistake but like you said, it should have crash. – danypata Jun 15 '13 at 22:19

2 Answers2

1

Try creating your UIStepper via this method:

UIStepper* stepper = [[UIStepper alloc] init];
stepper.frame = CGRectMake(220, 10, 100, 10);
[cell.contentView addSubview: stepper];

Which will put the stepper in the right of your table view cell.

And I found this answer in this related question.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Nope. And that wouldn't explain why the UISwitch worked, since it also had the CGRectZero. Changed it to all sorts of rect values, still doesn't show. – Andrew Jun 15 '13 at 21:40
  • The stepper size can't be changed `The bounding rectangle for a stepper matches that of a UISwitch object.` (UIStepper class reference), `A rectangle defining the frame of the UISwitch object. The size components of this rectangle are ignored.` (UISwitch initWithFrame reference). Second the stepper it is used as `accessoryView` so it is ok to have x=y=0. – danypata Jun 15 '13 at 21:40
  • thanks for the -1 @danypata; I did some additional research and found another related question which might help out the O.P. – Michael Dautermann Jun 15 '13 at 21:44
  • Sorry, by the -1, it was a mistake, I was trying to copy/paste the 'UIStepper' string from your answer :)) (and I'm from my phone) – danypata Jun 15 '13 at 21:45
  • Still no. I found that answer too. I actually copied and pasted the whole thing and it still didn't work. Then I found the UISwitch that I had working elsewhere and copied and pasted that and THAT worked. So I'm just confused. – Andrew Jun 15 '13 at 21:47
0

The code compiled in iOS 5.0, but the simulator was for iPhone 4.3. So the iPhone didn't know what UIStepper is, but knew what UISwitch was. Why it didn't cause the thing to crash (which could have saved me a lot of time) is a mystery.

Andrew
  • 1,571
  • 17
  • 31
  • It didnt crash because your XCode SDK knows about UIStepper but the 4.3 simulator does not. – Jasper Jun 17 '13 at 14:35
  • 1
    That explains why there's no compile-time error, but why not a runtime error? `aCell.accessoryView = stepperView; [stepperView setMaximumValue:10]; [stepperView setMinimumValue:0];` is essentially the equivalent of: `aCell.accessoryView = null; [null setMaximumValue:10]; [null setMinimumValue:0];` I think that should throw an exception. – Andrew Jun 17 '13 at 19:00