Vous pouvez utiliser UIStackView ou UITableView pour réaliser cette fonctionnalité.
Pour créer un UITextField, utilisez l'extrait suivant.
let tf = UITextField()
tf.borderStyle = .roundedRect
tf.setContentHuggingPriority(UILayoutPriority(integerLiteral: 750), for: .horizontal)
tf.setContentHuggingPriority(UILayoutPriority(integerLiteral: 750), for: .vertical)
En utilisant StackView,
- Pour ajouter UITextField, créez un champ de texte et l'ajouter en utilisant la méthode
func insertArrangedSubview(_ view: UIView, at stackIndex: Int)
. Plus d'informations peuvent être trouvées here. Pour supprimer tous les UITextFields, utilisez la méthode func removeArrangedSubview(_ view: UIView)
. Ici, vous devrez spécifier les vues que vous voulez supprimer. utilisez la propriété arrangedSubviews
de UIStackView pour obtenir un tableau de toutes les sous-vues. Plus d'informations peuvent être trouvées here
En utilisant UITableView,
Vous aurez besoin d'un UITableViewCell configuré pour avoir une UITextField en elle. Une fois que vous avez la cellule, gardez simplement une variable qui garde un compte de tous les UITextFields Chaque fois que l'utilisateur appuie sur le bouton plus, ajoutez un TextField en utilisant func insertRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation)
. N'OUBLIEZ PAS D'AUGMENTER VOTRE COMPTE. Plus d'informations peuvent être trouvées here. Vous pouvez également simplement augmenter la variable count et recharger la table.
Pour tout supprimer, vous pouvez utiliser func deleteRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation)
ou simplement définir la variable de comptage comme zéro et utiliser func reloadData()
. Plus d'informations peuvent être trouvées here et here
Vous pouvez vous épargner la peine d'ajouter des contraintes en utilisant 'UIStackView'. Ensuite, ajoutez simplement les nouveaux champs de texte à cela. Je ne sais pas par cœur comment faire ça mais je vais y jeter un coup d'oeil. Cela semble amusant. – Marmelador
https://stackoverflow.com/questions/1196436/uitextfield-in-uitableviewcell-adding-new-cells –