0

im making an app to sell tickets, there are different ticket types, for each ticket type i would like to ass a:

nameLabel UIStepper amountLabel priceLabel.

so those 4 views * ticket types.. added to the viewcontroller during runtime.

can i do this without doing it in a tableviewcontroller?

cant seem to add them dynamicly below eachother, any hints?

Kasper Sølvstrøm
  • 270
  • 1
  • 2
  • 22

2 Answers2

1

Im assuming your ticket is a UITableViewCell since you are mentioning "tableviewcontroller". If thats the case your should make a subclass of UITableViewCell and add those 4 views to the subclassed cell.

So that your subclassed cell header would look something like:

#Import <UIKit/UIKit.h>

@interface TicketCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIStepper *stepper;
@property (weak, nonatomic) IBOutlet UILabel *ammountLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@end

Then you should set this class as your UITableViews prototype cell class in Interface Builder and then drag and drop the UILabels and the UIStepper into the prototype cell. After that you need to connect the outlets to the right UILabels and UIStepper.

And in your uitableviewcontroller you want to reuse this prototype

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

    TicketCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

    if (cell == nil) {
        cell = [[TicketCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    return cell;
}

and finally you want to set the number of rows in your table to the number of objects in your "number-of-ticket-types-array" like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [ticketTypes count];
}
Calle Bergström
  • 480
  • 4
  • 12
0

if you don't want to use tableview you can do this by using UIScollView as follows

import "ViewController.h"

@interface ViewController ()
{
    float y;

}
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

- (IBAction)newTicket:(UIButton *)sender;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    y=50.0;


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)newTicket:(UIButton *)sender
{


    UILabel * nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10.0,y,50, 30)];
    UILabel * priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(10.0,y+32, 50, 30)];
    UILabel * amountLabel = [[UILabel alloc]initWithFrame:CGRectMake(10.0,y+64, 50, 30)];
    UIStepper * stepper = [[UIStepper alloc]initWithFrame:CGRectMake(10.0,y+96,50,30)];

    nameLabel.text = @"name";
    priceLabel.text = @"price";
    amountLabel.text = @"amount";

    [self.scrollView addSubview:nameLabel];
    [self.scrollView addSubview:priceLabel];
    [self.scrollView addSubview:amountLabel];
    [self.scrollView addSubview:stepper];

    y = y +130.0;

    if (y>=self.view.frame.size.height)
    {

        self.scrollView.contentSize =CGSizeMake(320.0, y+120.0);
    }
}
@end
Ravi
  • 2,441
  • 1
  • 11
  • 30
  • it's not that i have more tickets that fit the screen, it's just that i dont know how to make 3 times stepper+labels when there are 3 tickets, and 5 when 5 and so on.. – Kasper Sølvstrøm Jun 04 '14 at 07:32
  • in java i would have made a class with the parameters and a container with the UI elements and added that ontop of the main GUI, i just dont know if i can do something similar in IOS – Kasper Sølvstrøm Jun 04 '14 at 07:33
  • The classic way of doing that in iOS is based on the same principle as in android and is executed like my answer shows – Calle Bergström Jun 05 '14 at 06:00