我正在制作一个销售门票的应用程序,有不同的门票类型,对于每种门票类型,我都想要:
nameLabel UIStepper amountLabel priceLabel。
所以这 4 个视图 * 票证类型.. 在运行时添加到视图控制器。
我可以在不使用 tableviewcontroller 的情况下做到这一点吗?
似乎无法在彼此下方动态添加它们,有什么提示吗?
我假设您的票是 UITableViewCell,因为您提到了“tableviewcontroller”。如果是这种情况,您应该创建 UITableViewCell 的子类并将这 4 个视图添加到子类单元格中。
这样您的子类单元格标题将类似于:
#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
然后你应该在 Interface Builder 中将这个类设置为你的 UITableViews 原型单元类,然后将 UILabels 和 UIStepper 拖放到原型单元中。之后,您需要将插座连接到正确的 UILabel 和 UIStepper。
在你的 uitableviewcontroller 中你想重用这个原型
- (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;
}
最后你想将表中的行数设置为“number-of-ticket-types-array”中的对象数,如下所示:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [ticketTypes count];
}
如果您不想使用 tableview,您可以使用UIScollView
如下方式执行此操作
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