0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RealmからUISegmentedControlでデータを取得 

Posted at

今回の内容

  • Realmに保存済みのデータからUISegmentedControlで選択した値に関連したデータを取得

コードと簡単解説

Realm

  • UISegmentedControlの各Titleに表示する値はtagに保存
RealmDataSets
class RealmDataSets:Object{
    
    @objc dynamic var productName = String()
    @objc dynamic var janCode = String()
    @objc dynamic var deadlineDay = String()
    @objc dynamic var tag = String()   
  
}
  • 保存している tagから、同じ値が入らないように配列を作成。
  • UISegmentedControlのTitleに同じものが出来ないようにするためです。
RealmCRUDModel
    func readRealmTag(){
        
        do{
            let realm = try Realm()
            realmTagArray = []
            
            for readRealmTag in realm.objects(RealmDataSets.self){
                
                if realmTagArray.allSatisfy({$0 != readRealmTag.tag}) == true{
                    
                    realmTagArray.append(readRealmTag.tag)                    
                }              
            }
        }catch{
            
            //エラー処理など
        }
    }
  • UISegmentedControlが選択された時に使用します。
  • .filterを使用して、選択されたUISegmentedControlのTitleから取得する値を決めます。
RealmCRUDModel
    func filterTagReadRealm(selectTag:String){
        
        do{
            let realm = try Realm()
            self.filterTagReadResultArray = []
            
            realm.objects(RealmDataSets.self).filter("tag == '\(selectTag)'").forEach({
                
                self.filterTagReadResultArray.append(["RealmProductName":$0.productName,
                                                      "RealmJANCode":$0.janCode,
                                                      "RealmDeadlineDay":$0.deadlineDay])
                
            })
        }catch{
            
            //エラー処理など
        }
    }

UISegmentedControl

  • Segmentの作成
  • ここでreadRealmTag()filterTagReadRealm()を使います。
UISegmentedControl
import Foundation
import UIKit


class SegmentedControl{

    var uiSegmentControl = UISegmentedControl()    
    let realmCRUDModel = RealmCRUDModel()
    
}

extension SegmentedControl{
    
    func createSegment(targetView:UIView){
        
        realmCRUDModel.readRealmTag()
        
        uiSegmentControl.removeAllSegments()
        
        if realmCRUDModel.realmTagArray.count != 0{
        
           for segmentCount in 0...realmCRUDModel.realmTagArray.count - 1{
         
                uiSegmentControl.insertSegment(withTitle: realmCRUDModel.realmTagArray[segmentCount], at: segmentCount, animated: true)
                
           }
        }
        
        uiSegmentControl.frame = CGRect(x: targetView.bounds.minX + 5, y: targetView.bounds.minY + 97, width: targetView.frame.size.width - 10, height: 32)
        
        uiSegmentControl.selectedSegmentTintColor = UIColor(red: 1.0, green: 0.40, blue: 0.51, alpha: 1.0)

        uiSegmentControl.addTarget(self, action: #selector(tagSearch), for: .valueChanged) 

        targetView.addSubview(uiSegmentControl)
    }
}

    @objc func tagSearch(sender:UISegmentedControl){

        realmCRUDModel.filterTagReadRealm(selectTag:realmCRUDModel.realmTagArray[sender.selectedSegmentIndex])
      
    }

}

終わり

ご指摘、ご質問などありましたら、コメントまでお願い致します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?