2016-12-09 9 views
1

enter image description here私の要件は、ユーザーにすべての選択オプションを表示して、すべての選択肢を一度に見ることができるようにし、アンドロイドのスピナービューで完了したように選択できるようにすることです。現在、私はUIピッカービューを使用しています。私はそれのスクリーンショットを添付しています。私はスピナーが必要ですか、ピッカービューに現在3つのアイテムを表示している同じ高さのアイテムを同時に表示させたいですか?これは私のコードです:iOSでアンドロイドのようなスピナーを表示するにはどうすればいいですか?

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { 
      var myString = view as! UILabel! 

      var myStringLength = 0 

      if(view == nil) { 
       myString = UILabel() 
      } 
      myString?.textAlignment = .center 

      var rowText:String 

      if(pickerView==District){ 
       rowText=districts[row] 
      } 
      else if(pickerView==block) 
      { 
       rowText=blocks[row] 
      } 
      else if(pickerView==management) 
      { 
       rowText=managements[row] 
      } 
      else 
      { 
       rowText=categories[row] 
      } 

      var attributedRowText = NSMutableAttributedString(string: rowText) 
      var attributedRowTextLength = attributedRowText.length 

      attributedRowText.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSRange(location: 0, length: attributedRowTextLength)) 

      attributedRowText.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica", size: 8.0)!, range: NSRange(location: 0 ,length:attributedRowTextLength)) 

      myString!.attributedText = attributedRowText 

      return myString! 
     } 
+0

Vipulはデータをアンドロイドのスピナーのようにしたいので、UITableViewを追加する必要があります。これを使用すると、リストビューでデータを表示できます。 –

+0

'pickerView(_:rowHeightForComponent:)'デリゲートメソッドを見てください。 – rmaddy

+0

おかげで今それを調べる –

答えて

0

ドロップダウンとしてテーブルビューを使用することができます。

subCategoryTableはテーブル名です。そのドロップダウンボタンを押すと、このテーブルが作成されます。ここでsubCatgBtnをクリックすると、このコードを書くbtnです。

subCategoryTable.frame   = CGRectMake(self.subCatgBtn.frame.origin.x, self.subCatgBtn.frame.origin.y + self.subCatgBtn.frame.size.height, self.subCatgBtn.frame.size.width,CGFloat(numberofrows * 2)); 
    subCategoryTable.delegate  = self 
    subCategoryTable.dataSource = self 
    subCategoryTable.tag = 2 
    //subCategoryTable.rowHeight = self.subCatgBtn.frame.size.height 
    subCategoryTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")//this is needed when you use custom cell otherwise no need 
    subCategoryTable.separatorColor = UIColor.lightGrayColor() 
    subCategoryTable.allowsSelection = true 
    self.view.addSubview(subCategoryTable) 

CellforrowIndex:

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

    //  var cell = tableView.dequeueReusableCellWithIdentifier("cell")! 
    if tableView.tag == 2 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell 

     //do whatever you want to show. 
    } 
    } 

numberofrows方法:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if tableView.tag == 2 
    { 

    districts.count 
    } 
    } 

、ちょうどあなたが選択したものは何でもそのボタンに名前を設定しDidselect方法について。

私は自分のUIをどのようにデザインしているのかよくわかりません。このボタンをクリックすると、動的に作成されるドロップダウンリストとして使用できます。

+0

私はこれを今しようとしています –

+0

私は[email protected]でこれのためのサンプルプロジェクトを私に電子メールで送ることができます –

+0

私は私のアクティブなプロジェクトからコードを与えました。私は今あなたに最初に試してみることはできません私はあなたのために1つのサンプルプロジェクトを作成し、送信するよりもない場合。 –

0

ココアタッチフレームワークにスピナーのようなものはありません。 あなたができることは、UItableView

のカスタム実装を持つか、実装に役立つNIDropdownのようなものを使用することもできます。

+0

私はドロップダウンを見ています –

+0

NIDropdownは良いですが、それは客観的なcで実装されています。迅速に実装できますか? –

関連する問題