Hey all I am just wondering how can I use a search bar to reload data in carousel item without reloading all other items?
below is the code for the carousel:
to Note the concerned one is if == "Languages"
lazy var car:iCarousel = {
let view = iCarousel()
view.frame = CGRect(x:0, y:Int(CGFloat(Double(Int(Float(bounds.size.height*0.05))) + globW )) , width: Int(Float(bounds.size.width)*0.85), height: Int(Float(bounds.size.height)*0.75))
// view.type = .coverFlow
view.type = .wheel
view.isVertical = true
view.bounces = true
view.decelerationRate = 0.0
view.clipsToBounds=true
// view.isScrollEnabled = false
// view.backgroundColor = .red
return view
}()
below is the concerned case for the carousel item:
case "C":
let titleV:UILabel!
let titleOption:NSMutableAttributedString!
let fSAttr = [ NSAttributedString.Key.font: UIFont(name: "Helvetica", size: bounds.size.width*0.04)!,
NSAttributedString.Key.foregroundColor : UIColor.white,
]
titleOption = NSMutableAttributedString(string:currentField[index].placeholder!, attributes: fSAttr)
let widthFS = titleOption.width(containerHeight: v.bounds.width*0.5)
img.frame=CGRect(x: Double(v.bounds.width)*0.5-Double(bounds.size.width)*0.05, y: Double(v.bounds.height)*0.2, width:Double(bounds.size.width)*0.1, height: Double(bounds.size.width)*0.1)
img.image=UIImage(named: currentField[index].path!)?.resized(to: CGSize(width: CGFloat(bounds.size.width*0.1), height: CGFloat(bounds.size.width*0.1)))
v.addSubview(img)
titleV = UILabel(frame: CGRect(x:Int((v.bounds.width - widthFS)*0.5) , y: Int(-v.bounds.height*0.02), width: Int(Float(0.449*bounds.size.height)), height: Int(Float(0.567*bounds.size.height))))
titleV.backgroundColor = .clear
titleV.attributedText = titleOption
// v.bounds.height*0.33
// titleV.isEditable = false
v.addSubview(titleV)
currentCarView = v
if currentField[index].id == "Languages"{
let cont=UITextField()
// TRY TO RELOAD V AND VIEW
// find children and an id
// https://www.youtube.com/watch?v=mMr8fg5LZcU
// https://www.raywenderlich.com/4363809-uisearchcontroller-tutorial-getting-started
cont.tag = index
// cont.text = topkol
// cont.becomeFirstResponder()
cont.textColor = .white
cont.attributedPlaceholder = NSAttributedString(string: "Search", attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray])
cont.setBottomBorder(color:UIColor.gray.cgColor)
cont.frame = CGRect(x: CGFloat(Int(Float(car.bounds.width*0.4))), y: v.bounds.height*0.5 /*CGFloat(Int(Float(car.frame.height*0.5)))-25*/, width:CGFloat(Int(Float(car.bounds.width*0.6))), height: 50)
cont.backgroundColor = .clear
cont.addTarget(self, action: #selector(editSearch(_:)), for: UIControl.Event.editingChanged)
cont.addTarget(self, action: #selector(editSearch(_:)), for: .editingDidEndOnExit)
v.addSubview(cont)
}
for n in 0..<currentField[index].selections!.count{
let buttonVal = UIButton()
buttonVal.tag = index
buttonVal.accessibilityIdentifier = currentField[index].selections![n]
if currentField[index].id == "Languages"{
//replace with search array currentField[index].selections![n]
let languageComponent: [String] = currentField[index].selections![n].components(separatedBy: "-");
let lastElement = languageComponent[languageComponent.count - 1];
// let elem = languageComponent
//
let b = (n)/4
let x = 0.2 + Double(Int(Double((n))-floor(Double(b))*4))*0.2
let y = 0.1*floor(Double(b)) //0.05 0.55 0.1
// Double(v.bounds.height)*y
buttonVal.frame=CGRect(x: Double(v.bounds.width)*Double(round(x*10)/10)-12.5, y: Double(v.bounds.height)*y, width:Double(v.bounds.width)*0.11, height: Double(v.bounds.width)*0.11)
buttonVal.setTitle(countryFlag(country: lastElement), for: .normal)
Scroll.contentSize.height = CGFloat(Double(v.bounds.height)*y)+CGFloat(Double(v.bounds.height)*0.1)
Container.frame.size = CGSize(width: car.bounds.width, height: Scroll.contentSize.height)
Container.addSubview(buttonVal)
Scroll.addSubview(Container)
if !Scroll.isDescendant(of: v){
v.addSubview(Scroll)
}
// v.backgroundColor = .green
// print(countryName(countryCode: "DE")!)
} else {
let x = n == 0 ? 0.2 : n == 1 ? 0.5 : 0.8
// print("the W",v.bounds.width)
buttonVal.frame=CGRect(x: Double(v.bounds.width)*Double(x)-12.5, y: Double(v.bounds.height)*0.6, width:Double(v.bounds.width)*0.11, height: Double(v.bounds.width)*0.11)
let imageVal = UIImage(named: currentField[index].selections![n])?.resized(to: CGSize(width: CGFloat(bounds.size.width*0.1), height: CGFloat(bounds.size.width*0.1)))
buttonVal.setImage(imageVal!.resized(to: CGSize(width: v.bounds.width*0.075, height: v.bounds.width*0.075)), for: .normal)
v.addSubview(buttonVal)
}
buttonVal.layer.cornerRadius = v.bounds.width*0.055
if currentField[index].id == "Gender" {
if currentField[index].text == currentField[index].selections![n]{
//HERE
buttonVal.backgroundColor = .white
}
selArr.append(selButtons(name:buttonVal, tag:index))
} else {
for r in 0..<currentField[index].selected!.count{
if currentField[index].selections![n] == currentField[index].selected![r] {
buttonVal.backgroundColor = .white
}
}
}
buttonVal.addTarget(self, action: #selector(editTypeC(_:)), for: UIControl.Event.touchDown)
}
Here is the editfunction:
@objc func editSearch(_ sender:UITextField){
DispatchQueue.main.async { [self] in
sender.text = sender.text!
currentCarView.backgroundColor = .red
car.reloadData()
car.currentItemView?.reloadInputViews()
}
}
thank you all for your item, please. do suggest for better ways