在视图中包含多个标签或文本字段的最佳方法是什么?

时间:2016-02-27 06:06:06

标签: ios xcode swift

我需要创建一个包含多个UILabelUIbutton(约20个)的视图控制器,它会打开一个输入UITextField的警告框。这样做的最佳方法是什么?

另外,如果在UIScrollView中添加它们是合适的,因为我不能将所有20个放在故事板视图中,如何将它们连接到每个视图?

4 个答案:

答案 0 :(得分:5)

您应该使用表格视图。如果您只有20个这样的项目,那么您可以使用静态表格视图单元格在故事板中创建它们。

答案 1 :(得分:1)

如果您知道某些内容会重复某些n times,那么将它们添加到视图中的最佳方法是使用UITableView(用于垂直视图)或UICollectionView(用于类似网格的视图),但是,如果项目不同且不重复,则显示它们的最佳情况是使用UIScrollView。你添加一个带有scrollView的UIView作为它的孩子。

之后,您可以在UIScrollView中添加更多子视图,并在更多项目开始添加时立即增加Scrollview的尺寸。要增加ScrollView高度或说宽度,或两者都增加,请将contentSizeviewWillLayoutSubviews()一起使用。结构可能如下所示:

View
--UIScrollView
---UILabel1
---UIImageView
---UITextView
---UIButton

要创建布局,有以下几种方法: 1.您可以在simulated size下将size inspector设置为Freeform,并选择高度和宽度,然后添加子视图,例如UITextView,UILabel等。 2.以编程方式添加UIScrollView,然后将其他视图添加为滚动的子视图。

希望它有助于使您成为正确的选择。

感谢。

答案 2 :(得分:1)

由于Pradeep非常简单地为您提供解决方案,我尝试了,我得到了它。我在下面发布了答案。它工作正常。

目标C

ViewController.m

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
   NSMutableArray *array;
}
@property (strong, nonatomic) IBOutlet UITableView *tableViewData;

@end

@implementation ViewController

@synthesize tableViewData;
- (void)viewDidLoad {
  [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
   array = [[NSMutableArray alloc]initWithObjects:@"iOS",@"Android",@"Windows",@"Blackberry",@"iPad",@"iPod",@"iWatch",@"iTV",@"iPhone",@"Tablet",@"Lenova",@"Microsoft",@"Honor",@"Samsung",@"Mi4",@"Moto",@"Lava",@"Nokia",@"ASus",@"OnePlus", nil];
}

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

//UITableView DataSource methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *strCell = @"cell";
  UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:strCell];
  if(cell==nil)
  {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
  }
  cell.textLabel.text = array[indexPath.row];
  return cell;
}

//UITableView Delegates Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Input" message:@"TextField" preferredStyle:UIAlertControllerStyleAlert];
   [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
   {
     textField.placeholder = NSLocalizedString(@"Enter the Product Name", @"Login");
     textField.text = array[indexPath.row];
   }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
    {
     textField.placeholder = NSLocalizedString(@"Enter the Password", @"Password");
     textField.secureTextEntry = YES;
    }];
   UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
   [alertController addAction:ok];
   [self presentViewController:alertController animated:YES completion:nil];
}
@end

SWIFT

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet var tableViewData: UITableView!

var array : [String] = ["iOS","Android","Windows","Blackberry","iPad","iPod","iWatch","iTV","iPhone","Tablet","Lenova","Microsoft","Honor","Samsung","Mi4","Moto","Lava","Nokia","ASus","OnePlus"]
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as UITableViewCell?

    if (cell == nil) {
        cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
    }

    cell?.textLabel?.text = array[indexPath.row] as String
    return cell!
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{

    let alertController = UIAlertController(title: "Input", message: "TextField", preferredStyle: .Alert)
    var loginTextField: UITextField?
    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
        // Enter the textfiled customization code here.
        loginTextField = textField
        loginTextField?.placeholder = "Enter the product"
        loginTextField?.text = self.array[indexPath.row]
    }
    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(defaultAction)

    presentViewController(alertController, animated: true, completion: nil)
}
}

答案 3 :(得分:0)

有三种一般方法。

  1. 滚动型。
  2. TableView中。
  3. StackView(仅限iOS 9)
  4. <强>滚动型

    请参阅Fennec的答案。

    <强>的TableView

    TableView有两种方法:StaticDynamic。 如果您始终确定有20个按钮/标签,则可以使用静态TableView,并且您不会面对您在IB中提到的有关scrollView的问题(因为tableViews在IB中可滚动)。但如果你不确定总有20个按钮/标签,那么使用Dynamic tableViews会更好。 user3182143的答案涵盖了这一点。

    <强> StackView

    参见本教程 - &gt; Introducing Stack Views

    但如果是我,我会使用Dynamic TableView。

相关问题