I am unable to select future dates from the date picker view. I would like to be able to select only current and future dates, not past dates.
{
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: EnterAccountCell2.self), for: indexPath) as! EnterAccountCell2
cell.inputTextField.title = NSLocalizedString("StartDate", comment: "")
cell.inputTextField.delegate = self
cell.inputTextField?.placeholder = NSLocalizedString("PleaseSelect", comment: "")
cell.inputTextField.titleFont = UIFont(name: UIConfiguration.getUIFONTAPP(), size: UIConfiguration.kFontSizeMedium)!
cell.inputTextField.tag = 222
cell.inputTextField.inputView = UIView(frame: .zero)
cell.inputTextField.text = self.myDateShow
cell.datePickerView.addTarget(self, action: #selector(datePickerChanged(picker:)), for: .valueChanged)
cell.selectView.isHidden = false
cell.inputTextField.isUserInteractionEnabled = true
cell.datePickerView.minimumDate = Date()
cell.datePickerView.maximumDate = Date(timeInterval: 10976*24*60*60, since: Date())
return cell
}
To limit the date picker view to only display current and future dates, you need to set the minimumDate property of the datePickerView to the current date. In the code you provided, this is done with the following line:
cell.datePickerView.minimumDate = Date()
Example:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: EnterAccountCell2.self), for: indexPath) as! EnterAccountCell2
cell.inputTextField.title = NSLocalizedString("StartDate", comment: "")
cell.inputTextField.delegate = self
cell.inputTextField?.placeholder = NSLocalizedString("PleaseSelect", comment: "")
cell.inputTextField.titleFont = UIFont(name: UIConfiguration.getUIFONTAPP(), size: UIConfiguration.kFontSizeMedium)!
cell.inputTextField.tag = 222
cell.inputTextField.inputView = UIView(frame: .zero)
cell.inputTextField.text = self.myDateShow
cell.datePickerView.addTarget(self, action: #selector(datePickerChanged(picker:)), for: .valueChanged)
cell.selectView.isHidden = false
cell.inputTextField.isUserInteractionEnabled = true
cell.datePickerView.minimumDate = Date()
cell.datePickerView.maximumDate = Date(timeInterval: 10976*24*60*60, since: Date())
return cell
}
Related
In my project I have a SignUpViewController which looks like this:
All the textFields are custom-cells within a tableViewController.
TableView:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// 1st cell -> email textfield
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpEmailCell", for: indexPath) as! SignUpEmailCell
return cell
// 2nd cell -> anzeigename
}else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpAnzeigeName", for: indexPath) as! SignUpAnzeigeName
return cell
// 3rd cell -> Wishlist-Handle
}else if indexPath.row == 2 {
let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpHandleCell", for: indexPath) as! SignUpHandleCell
return cell
// 4th cell -> passwort textfield
}else if indexPath.row == 3 {
let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpPasswordCell", for: indexPath) as! SignUpPasswordCell
return cell
// 5th cell -> repeat password textfield
}else if indexPath.row == 4 {
let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpPasswordRepeatCell", for: indexPath) as! SignUpPasswordRepeatCell
return cell
// 6th cell -> document label
}else if indexPath.row == 5 {
let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpDocumentCell", for: indexPath) as! SignUpDocumentCell
return cell
}
// last cell -> signUpButton
let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpButtonCell", for: indexPath) as! SignUpButtonCell
return cell
}
Password-Cell: (basic structure is the same for every cell)
class SignUpPasswordCell: UITableViewCell, UITextFieldDelegate {
public static let reuseID = "SignUpPasswordCell"
lazy var eyeButton: UIButton = {
let v = UIButton()
v.addTarget(self, action: #selector(eyeButtonTapped), for: .touchUpInside)
v.setImage(UIImage(named: "eyeOpen"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
lazy var passwordTextField: CustomTextField = {
let v = CustomTextField()
v.borderActiveColor = .white
v.borderInactiveColor = .white
v.textColor = .white
v.font = UIFont(name: "AvenirNext-Regular", size: 17)
v.placeholder = "Passwort"
v.placeholderColor = .white
v.placeholderFontScale = 0.8
v.minimumFontSize = 13
v.borderStyle = .line
v.addTarget(self, action: #selector(SignUpPasswordCell.passwordTextFieldDidChange(_:)),for: .editingChanged)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.backgroundColor = .clear
passwordTextField.delegate = self
eyeButton.isHidden = true
passwordTextField.textContentType = .newPassword
passwordTextField.isSecureTextEntry.toggle()
setupViews()
}
func setupViews(){
contentView.addSubview(passwordTextField)
contentView.addSubview(eyeButton)
passwordTextField.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
passwordTextField.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
passwordTextField.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
passwordTextField.heightAnchor.constraint(equalToConstant: 60).isActive = true
eyeButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -5).isActive = true
eyeButton.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 10).isActive = true
}
var check = true
#objc func eyeButtonTapped(_ sender: Any) {
check = !check
if check == true {
eyeButton.setImage(UIImage(named: "eyeOpen"), for: .normal)
} else {
eyeButton.setImage(UIImage(named: "eyeClosed"), for: .normal)
}
passwordTextField.isSecureTextEntry.toggle()
if let existingText = passwordTextField.text, passwordTextField.isSecureTextEntry {
/* When toggling to secure text, all text will be purged if the user
continues typing unless we intervene. This is prevented by first
deleting the existing text and then recovering the original text. */
passwordTextField.deleteBackward()
if let textRange = passwordTextField.textRange(from: passwordTextField.beginningOfDocument, to: passwordTextField.endOfDocument) {
passwordTextField.replace(textRange, withText: existingText)
}
}
/* Reset the selected text range since the cursor can end up in the wrong
position after a toggle because the text might vary in width */
if let existingSelectedTextRange = passwordTextField.selectedTextRange {
passwordTextField.selectedTextRange = nil
passwordTextField.selectedTextRange = existingSelectedTextRange
}
}
#objc func passwordTextFieldDidChange(_ textField: UITextField) {
if textField.text == "" {
self.eyeButton.isHidden = true
}else {
self.eyeButton.isHidden = false
}
}
}
Problem:
I would like to be able to show some extra information on some textFields when selected.
For example: When passwordTextField is editing I would like to show the password requirements right below the textfield. But the extra information should only be displayed while editing or after editing. When the ViewController is being displayed at first it should still look like the picture above.
I hope my problem is clear and I am grateful for every help.
I have checkbox and label inside a tableview and when we click checkbox the price present in label in each cell of tableview should add to another label which is present in another view
#IBAction func checkUncheckButtonAction(_ sender: UIButton) {
if let cell = sender.superview?.superview as? PrepaidPageTableViewCell
{
let indexPath = tableviewOutlet.indexPath(for: cell)
if cell.checkUncheckButtonOutlet.isSelected == false
{
cell.checkUncheckButtonOutlet.setImage(#imageLiteral(resourceName: "checked_blue"), for: .normal)
cell.checkUncheckButtonOutlet.isSelected = true
viewHeightConstraint.constant = 65
cell.amountOutlet.text = "₹ "+amount_receivable_from_customerArray[indexPath!.row]
isPrepaidOrder = false
tableviewOutlet.reloadData()
} else {
cell.checkUncheckButtonOutlet.setImage(#imageLiteral(resourceName: "unchecked_blue"), for: .normal)
cell.checkUncheckButtonOutlet.isSelected = false
self.viewHeightConstraint.constant = 0
tableviewOutlet.reloadData()
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PrepaidPageTableViewCell") as! PrepaidPageTableViewCell
cell.customerNameOutlet.text = buyer_nameArray[indexPath.row]
cell.deliverydateOutlet.text = "Delivery Date:\(dispatch_dateArray[indexPath.row])"
cell.amountOutlet.text = "₹\(amount_receivable_from_customerArray[indexPath.row])"
cell.dispatchidoutlet.text = "Dispatch ID: \(id_dispatch_summaryArray[indexPath.row])"
cell.dispatchdateOutlet.text = "Dispatch Date:\(dispatch_dateArray[indexPath.row])"
cell.checkUncheckButtonOutlet.setImage(#imageLiteral(resourceName: "unchecked_blue"), for: .normal)
cell.selectionStyle = .none
return cell
}
I have a uicollectionview with collectionviewcells and each cell has a boolean value associated with a favorites button. There are over 50 cells positioned vertically (four cells are viewable at a time). If the favorite button is clicked it toggles between a highlighted image and a non-highlighted image.
That functionality works, but for some reason when I click one then scroll down I see other cells with their favorite button highlighted. When I scroll back up the cell favorite button is no longer highlighted.
Is there something missing from this code?
NOTE:: As a default I set each cell's boolean value to false. It's only changed when I click on the cell's favorite button.
My code below:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SimpleDispensarySubCell
cell.backgroundColor = UIColor.init(white: 0.10, alpha: 0.25)
cell.infoLine2TextVw.text = ""
cell.infoLine3TextVw.text = ""
if let heading_name = self.dict_dict_holder[indexPath.item]["Name"]{
cell.headerTextVw.text = heading_name
cell.infoLine1TextVw.text = self.dict_dict_holder[indexPath.item]["Phone"]
}
if cell.isFavorite{
cell.isFavorite = true
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_fill_icon"), for: .normal)
}
else{
cell.isFavorite = false
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_nofill_icon"), for: .normal)
}
cell.bringSubview(toFront: cell.headerTextVw)
//cell.favorite_button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(AddFavorite(withSender:))))
cell.favorite_button.addTarget(self, action:#selector(AddFavorite), for: .touchUpInside)
return cell
}
#objc func AddFavorite(withSender sender:UIButton){
let cell = sender.superview as! SimpleDispensarySubCell
if cell.isFavorite{
cell.isFavorite = false
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_nofill_icon"), for: .normal)
}
else{
cell.isFavorite = true
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_fill_icon"), for: .normal)
}
}
It's because you are using collectionView.dequeueReusableCell you should define an array to hold favorite state of each cell on it. It could solve your problem.
let favoriteStateArray = countOfRows;
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SimpleDispensarySubCell
cell.backgroundColor = UIColor.init(white: 0.10, alpha: 0.25)
cell.infoLine2TextVw.text = ""
cell.infoLine3TextVw.text = ""
if let heading_name = self.dict_dict_holder[indexPath.item]["Name"]{
cell.headerTextVw.text = heading_name
cell.infoLine1TextVw.text = self.dict_dict_holder[indexPath.item]["Phone"]
}
if favoriteStateArray[indexPath.row]{
cell.isFavorite = true
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_fill_icon"), for: .normal)
}
else{
cell.isFavorite = false
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_nofill_icon"), for: .normal)
}
cell.bringSubview(toFront: cell.headerTextVw)
//cell.favorite_button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(AddFavorite(withSender:))))
cell.favorite_button.addTarget(self, action:#selector(AddFavorite), for: .touchUpInside)
return cell
}
#objc func AddFavorite(withSender sender:UIButton){
let cell = sender.superview as! SimpleDispensarySubCell
let index = collectionView.indexPath(for: cell)
if favoriteStateArray[indexPath.row]{
favoriteStateArray[indexPath.row] = false
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_nofill_icon"), for: .normal)
}
else{
favoriteStateArray[indexPath.row] = false
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_fill_icon"), for: .normal)
}
}
Since the cells are getting reused changes are affecting multiple cells.
You can use prepare for reuse method to clear changes that must affect only a particular cell and Clear the cell and prepare it for Reuse.
override func prepareForReuse() {
cell.isFavorite = false
super.prepareForReuse()
}
I have a keyboard extension in iOS 11 that includes a collection view of articles coming in from JSON. I have a button in the prototype cell that I would like to allow a user to press to open the article in Safari external to the keyboard. I can get it to open all links in a static URL, but I cant get it to open each article's URL. What am I missing?
I've put an example of the working simple static action and also included what I have tried but doesn't work in this code:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if(collectionView == self.key.colImages)
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gifCollectionViewCell", for: indexPath) as! gifCollectionViewCell
cell.lblTitle.text = self.articles[indexPath.row].headline
let prefix: String = "https://res.cloudinary.com/djvbbwrnm/image/fetch/"
let options: String = "w_0.2/"
if let imageURL = self.articles[indexPath.row].imageURL
{
let articleURL = self.articles[indexPath.row].url
let url = URL(string: articleURL!)
let urlAppended = prefix+options+imageURL
cell.imgView.sd_setImage(with: URL(string: urlAppended), completed: nil)
//This works
cell.shareButton.addTarget(self, action: #selector(openLink), for: .touchUpInside)
//This doesn't
cell.shareButton.addTarget(self, action: #selector(openUrl(url: url)), for: .touchUpInside)
}
return cell
}
else
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "catCollectionViewCell", for: indexPath) as! catCollectionViewCell
cell.imgView.image = buttPics[indexPath.row]
cell.imgView.layer.cornerRadius = 2
cell.imgView.layer.masksToBounds = true
return cell
}
}
#objc func openLink(){
let articleURL = "http://google.com"
let url = URL(string: articleURL)
openUrl(url: url)
}
#objc func openUrl(url: URL?) {
let selector = sel_registerName("openURL:")
var responder = self as UIResponder?
while let r = responder, !r.responds(to: selector) {
responder = r.next
}
_ = responder?.perform(selector, with: url)
}
You cant add any other DataTypes as arguments. Because, you are adding addTarget for UIButton.
#objc func openLink(){
}
#objc func openLink(sender: UIButton){ // URL is not possible.
}
The above two codes are same. In second one, you can access that UIButton's property.
Runnable Code
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if(collectionView == self.key.colImages)
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gifCollectionViewCell", for: indexPath) as! gifCollectionViewCell
cell.lblTitle.text = self.articles[indexPath.row].headline
let prefix: String = "https://res.cloudinary.com/djvbbwrnm/image/fetch/"
let options: String = "w_0.2/"
if let imageURL = self.articles[indexPath.row].imageURL
{
//let articleURL = self.articles[indexPath.row].url
//let url = URL(string: articleURL!)
let urlAppended = prefix+options+imageURL
cell.imgView.sd_setImage(with: URL(string: urlAppended), completed: nil)
//This works
cell.shareButton.addTarget(self, action: #selector(openLink), for: .touchUpInside)
//This doesn't
//cell.shareButton.addTarget(self, action: #selector(openUrl(url: url)), for: .touchUpInside)
cell.shareButton.tag = indexPath.row // SET TAG TO UIBUTTON
}
return cell
}
else
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "catCollectionViewCell", for: indexPath) as! catCollectionViewCell
cell.imgView.image = buttPics[indexPath.row]
cell.imgView.layer.cornerRadius = 2
cell.imgView.layer.masksToBounds = true
return cell
}
}
#objc func openLink(sender: UIButton){ // USE THIS.
let buttonTag : Int = sender.tag
let articleURL = self.articles[buttonTag].url
let url = URL(string: articleURL!)
// You can achieve by this way.
// Since I am in a keyboard extension, I added the follwoing code and it is working now.
let selector = sel_registerName("openURL:")
var responder = self as UIResponder?
while let r = responder, !r.responds(to: selector) {
responder = r.next
}
_ = responder?.perform(selector, with: url)
}
I have a tableview with 2 textbox.text1 loads data on viewdidload function.
for text2, the input is dates. I am using tableview didselectrow method to show datepicker. but when i select date from datepicker, it is printing in console but not added in textbox.
` override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedIndex = indexPath.row
let screenSize: CGRect = UIScreen.mainScreen().bounds
datePickerContainer.frame = CGRectMake(20, 200, 290, 200.0)
datePickerContainer.backgroundColor = UIColor.whiteColor()
datePickerContainer.layer.borderWidth = 2.0
datePickerContainer.layer.borderColor = UIColor.lightGrayColor().CGColor
var pickerSize : CGSize = datePicker.sizeThatFits(CGSizeZero)
datePicker.frame = CGRectMake(0.0, 20, pickerSize.width, 160)
//datePicker.setDate(NSDate(), animated: true)
//datePicker.maximumDate = NSDate()
datePicker.datePickerMode = .Date
datePicker.addTarget(self, action: "dateChangedInDate:", forControlEvents: UIControlEvents.ValueChanged)
datePickerContainer.addSubview(datePicker)
var doneButton = UIButton()
doneButton.setTitle("Done", forState: UIControlState.Normal)
doneButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
doneButton.addTarget(self, action: Selector("dismissPicker:"), forControlEvents: UIControlEvents.TouchUpInside)
doneButton.frame = CGRectMake(130.0, 160, 70.0, 37.0)
datePickerContainer.addSubview(doneButton)
self.view.addSubview(datePickerContainer)
}
func dateChangedInDate(sender:UIDatePicker){
dateFormatter.dateStyle = .ShortStyle
dateFormatter.dateFormat = "yyyy-MM-dd"
let selectedDate = dateFormatter.stringFromDate(datePicker.date)
firstIndex = NSIndexPath(forRow: selectedIndex, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(firstIndex) as! mytableviewcell
cell.dateText.text = dateFormatter.stringFromDate(datePicker.date)
// print(selectedDate)
}
`
Any help is appreciated....
You should try making all updates to cells inside the tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell method. Make it so that the text of your label is bound to the DatePicker value. Then, inside your dataChangedWithDate() method, trigger a table update of the row in question using table.reloadRowsAtIndexPath(). Note that I'm assuming selectedIndex is an instance variable for your controller, so that you have access to it inside the dateChangedWithDate() method.
tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Dequeue the cell, perform all of your other tasks for laying out the cell
// {...}
// Bind the label's text to the DatePicker value
dateFormatter.dateStyle = .ShortStyle
dateFormatter.dateFormat = "yyyy-MM-dd"
cell.dateText.text = dateFormatter.stringFromDate(datePicker.date)
}
func dateChangedInDate(sender:UIDatePicker){
// Trigger table update for the selected row
let indexPath = NSIndexPath(forRow: selectedIndex, inSection:0)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
}
create Variable to store data value.
// var dateTime = ""
//cellForIndexMethod
cell.dateText.text = dateTime
func dateChangedInDate(sender:UIDatePicker){
dateFormatter.dateStyle = .ShortStyle
dateFormatter.dateFormat = "yyyy-MM-dd"
dateTime = dateFormatter.stringFromDate(datePicker.date)
let indexPath = NSIndexPath(forRow: selectedIndex, inSection:0)
//Reload table
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
}