1

im trying to add a UIStepper programmatically, HEre is my code: the stepper wont show up. :

     stepper =  [[UIStepper alloc]init];
     [stepper setFrame:CGRectMake(216, 91, 155, 25)];
     [stepper setMinimumValue:0];
     [cell addSubview:stepper];

Thank you!

CELLFORROWATINDEXPATH:

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

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
     {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

         UIStepper * myStepper =  [[UIStepper alloc]initWithFrame:CGRectMake(206,20,94,27)];
         [myStepper setMinimumValue:0];
         [cell addSubview:myStepper];

         cellLabel = [[UILabel alloc]init];
         [cellLabel setFrame:CGRectMake(65, 22, 27, 27)];
         [cellLabel setText:@"1"];
         [cell.contentView addSubview:cellLabel];
    } 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.imageView.image = [imageArray objectAtIndex:indexPath.row];    
   // cell.textLabel.text = [cells objectAtIndex:indexPath.row];
    if ([cell.imageView.image  isEqual:[UIImage imageNamed:@"paddle5.png"]]) {
        [cellLabel setFrame:CGRectMake (175, 22, 30, 30)];
    }
    if ([cell.imageView.image  isEqual:[UIImage imageNamed:@"paddle4.png"]]) {
        [cellLabel setFrame:CGRectMake (150, 22, 30, 30)];
    }
    if ([cell.imageView.image  isEqual:[UIImage imageNamed:@"paddle3.png"]]) {
        [cellLabel setFrame:CGRectMake (120, 22, 30, 30)];
    }
    if ([cell.imageView.image  isEqual:[UIImage imageNamed:@"paddle2.png"]]) {
        [cellLabel setFrame:CGRectMake (90, 22, 30, 30)];
    }
return cell;
}

the if statements right above return cell; is just to position a few labels i have on the cell.

iProRage
  • 424
  • 7
  • 19

3 Answers3

2
[cell.contentView addSubview:stepper];

EDIT: In your code, you have:

 [stepper setFrame:CGRectMake(216, 91, 155, 25)];

Yet you claim that your cell is 73px tall and 320px wide.

Currently, you are setting the stepper to be 155px wide, 25px tall, at an x origin of 216 and a y origin of 91.

The stepper, appearing at y of 91 is outside of the view of the cell.

Try this:

[stepper setFrame:CGRectMake(216, 22, 100, 30)];
Daniel Amitay
  • 6,677
  • 7
  • 36
  • 43
  • doesnt work :( does it have somehting to do with my setFrame:CGRect? THanks – iProRage Dec 02 '11 at 06:30
  • 2
    What is the size of your cell? Does that frame fit within the bounds of the cell? – ThomasW Dec 02 '11 at 07:07
  • @ThomasW the height is 73px tall and 320px wide!! – iProRage Dec 06 '11 at 03:54
  • @iProRage, please take a look at my edit. You were displaying the stepper outside of the cell... – Daniel Amitay Dec 06 '11 at 10:59
  • @DanielAmitay okay i tried this, however it still didnt show up :( i dont know why it wont. I ran a new project and it worked fine. It showed up perfectly, but when i add the exact same code to the `cellForRow` method, it doesnt show up in this app. I added all of my `cellForRow` in my question. Would you please take a look at it and see if there is anything wrong with it? Thanks for the help!! – iProRage Dec 07 '11 at 04:16
  • in my code, i added `NSLog(@"The stepper is %@", stepper);` and the output was `the stepper is (NULL)` what does this mean? – iProRage Dec 07 '11 at 23:48
2

Use initWithFrame:, that is the designated initializer when instantiating a class that is a subclass of UIView programmatically.

So: (adjusting frame for standard 44 points high cell)

stepper =  [[UIStepper alloc]initWithFrame:CGRectMake(206, 8, 94, 27)];

I'm not sure why it's still not showing up for you. To test the code I created a new Xcode project of type "Master/Detail" targeted for iPhone, then the only thing I did was to change the tableView:cellForRowAtIndexPath: method like so.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(206, 8, 94, 27)];
        [stepper setMinimumValue:0];
        [cell addSubview:stepper];
    }
    // Configure the cell.
    cell.textLabel.text = NSLocalizedString(@"Detail", @"Detail");
    return cell;
}

For me this code shows the default project table and the "Detail" row has a stepper in it.

NJones
  • 27,139
  • 8
  • 70
  • 88
  • thank you for the reply, however i added this line and removed my `stepper = [[UIStepper alloc]init];` declaration. And it still refuses to show up! Do you know why this is? Thanks – iProRage Dec 04 '11 at 03:20
  • im sorry, its not 44px tall, i was being dumb, in interface builder, i set the height to 73, not 43 – iProRage Dec 04 '11 at 03:34
  • haha damn man!! :/ i fixed the code to the method you stated above and i fixed the coordinates. It still wont show up! – iProRage Dec 04 '11 at 03:42
  • Slap this in before the code `NSLog(@"The cell is:%@",cell);` – NJones Dec 04 '11 at 06:53
  • okay, i got it and the console said: `The cell is:>` – iProRage Dec 04 '11 at 18:41
  • Okay, a few things, first of all your cell appears to be 44 points tall here. Not that that should matter since the stepper would still be mostly visible. Second to be honest I thought for sure your 'cell' was nil, and I am quite surprised that it is a valid tableview cell. I suppose my next question would be in what method are you using this code? – NJones Dec 04 '11 at 22:22
  • in the cellForRowAtIndexPath method! inside the if statement: if(cell == nil){code is here} And yea i was thinking somehow my stepper was nil. – iProRage Dec 06 '11 at 00:30
  • hey i just wanted to say thank you so much for all of the help! :) and i also wanted to ask: you do know that this stepper is in a tableViewCell right? Would i need any other code? Thank you so much! – iProRage Dec 06 '11 at 01:03
  • I ran a test, and it shows for me. Posted code for you to compare. – NJones Dec 06 '11 at 02:40
  • this is very weird, i ran the same exact text as you, and it worked, the stepper showed up. But i put the EXACT same code in my project that im building and it still refuses to show up. Im totally stumped!! :/ if i post my entire `cellForRow` method could you take a quick look and see anything popping out that could be wrong please? Ill post it now! :D Thanks a bunch!!!! – iProRage Dec 06 '11 at 03:46
1

UIStepper control is introduced in ios 5.0 .you need to check you are not using it below ios 5.0.

For more references :-

UIStepperControlExample

UIStepperControlAppleDocumentation

Gypsa
  • 11,230
  • 6
  • 44
  • 82