Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top