hi i have text field in uicollectionviewcell
so what i need it example :
when i edit text filed value in row 5 and i done it and go to text filed in row 20 to edit value the collectionview has reloaded and forget value in row 5,
so i need way to save value temporarily while do I change it manually
this my code :
cell.foodNumber.tag = indexPath.row
if let foodcodes = self.menu![indexPath.row]["code"] as? NSString {
if contains(self.indexPathsForSelectedCells, indexPath) {
cell.currentSelectionState = true
cell.foodNumber.enabled = true
cell.foodNumber.text = "1"
println("foods:\(foodcodes) Count:\(cell.foodNumber.text)")
println(cell.foodNumber.tag)
} else {
cell.foodNumber.enabled = false
cell.foodNumber.text = nil
}
}
Implement in your ViewController the UITextFieldDelegate protocol, specifically the textField:didEndEditing method.
Save your indexPath.row in the textField.tag as you're doing, and set the delegate to the controller, where you can save the value.
This is a very simplistic example:
class MyViewController : UITableViewController {
var texts = [Int:String]()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier( "Cell" ) as! UITableViewCell
cell.textField.delegate = self
cell.textField.tag = indexPath.row
// restore saved text, if any
if let previousText = texts[indexPath.row] {
cell.textField.text = previousText
}
else {
cell.textField.text = ""
}
// rest of cell initialization
return cell
}
}
extension MyViewController : UITextFieldDelegate {
func textFieldDidEndEditing(textField: UITextField) {
// save the text in the map using the stored row in the tag field
texts[textField.tag] = textField.text
}
}
Related
i have this problem, I have a table view with a custom cell, the custom cell has a text field, I should put text in this textfield with a custom button, the button is out of the tableview.
my problem is, how I can set text into textfield? and my other problem is that I can't identify the current textfield for set text, I only know the tag.
this is my code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! budgetCellTableViewCell
cell.insertText.delegate = self
cell.insertText.tag = indexPath.row
cell.insertText.inputView = UIView()
cell.showViews()
if let money = arrayData[indexPath.row]["amount"]
{
cell.showSubtitle.text = "Ultimos 30 dias: \(money)"
}
if let cat = arrayData[indexPath.row]["category"]
{
cell.showTitle.text = cat as? String
}
cell.selectionStyle = .none
return cell
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if self.textFieldTagActive == textField.tag
{
textField.text = setText
}
else
{
self.setText = ""
self.textFieldTagActive = textField.tag
}
func addNumber(_ number: Int) {
if number != -1
{
setText += String(number)
}
else
{
setText.removeLast()
}
}
the function addNumber is used when I push my custom button
with the function textFieldDidBeginEditing I get the textfield tag, I push my custom button and finally I push the same textfield and the text appears in the textfield, but I really want push my button and the same time, the text appears into textfield
so, how I can set text into my textfield since my custom button that is out of tableview?
thanks
you can get the current text field from delegate functions.
class ViewController: UIViewController {
var currentText = "your text"
var currentTF: UITextField?
#IBAction func buttonTapped(_ sender: Any) {
// set text to the text field.
currentTF?.text = currentText
}
}
extension ViewController: UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
// get the text field that you want
currentTF = textField
return true
}
}
I have a tableView who need to contain two different view, the name of the first one is CustomTableViewCell the second one is CustomDeliveryTableViewCell
I want my variable to take the two cell, I don't understand this error.
Here my function
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell: UITableViewCell
cell = Bundle.main.loadNibNamed("CustomTableViewCell", owner: self, options: nil)?.first as! UITableViewCell
if (!self.appReady)
{
return cell
}
let arrayOfCard = self.selectedCard(section: indexPath.section, row: indexPath.row)
let json:JSON = JSON(arrayOfCard)
if (json[0]["cards"][indexPath.row]["category"] == "delivery")
{
cell = Bundle.main.loadNibNamed("CustomDeliveryTableViewCell", owner: self, options: nil)?.first as! CustomTableViewCell
}
cell = fillCell(cell: cell, json: json, index: indexPath.row)
return cell
}
My fonction fillCell is prototype like that
func fillCell(cell: CustomTableViewCell, json:JSON, index:Int) ->
CustomTableViewCell
Edit
Here the code of actual fillCell function
func fillCell(cell: UITableViewCell, json:JSON, index:Int) -> UITableViewCell {
if (json[0]["cards"][index]["category"] == "train")
{
if let type = json[0]["cards"][index]["category"] as JSON?
{
cell.labelType.text = type.string
}
if let departureStation = json[0]["cards"][index]["train"]["departure"]["station"] as JSON?
{
cell.labelDepartureStation.text = departureStation.string
}
// Do some code
}
else if (json[0]["cards"][index]["category"] == "delivery")
{
//Do some code
return cell
}
else{
//Do some code
return cell
}
}
Your initial assignment creates a cell of type CustomDeliveryTableViewCell.
Within the if block you're trying to assign a CustomTableViewCell to the same variable. This will only work if CustomTableViewCell is a subclass of CustomDeliveryTableViewCell
When you call fillCell( it's expecting CustomTableViewCell, but cell is a CustomDeliveryTableViewCell
If you declare var cell: UITableViewCell then you can assign either type to it.
I have used a tableview with 16 cells on a view controller. Each cell has a textfield and a picker view as a inputview for textfield. The odd thing is that When I choose the value for the first cell, it's fine. When I scrolled down to the last cell, the value is same as the first one. But I have never touched the last cell. Why would this happened?
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
// selected value in Uipickerview in Swift
answerText.text = pickerDataSource[row]
answerText.tag = row
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = myTable.dequeueReusableCellWithIdentifier("addFollowCell", forIndexPath: indexPath) as! AddFollowTableViewCell
cell.questionView.text = listQuestion1[indexPath.row]
cell.pickerDataSource = dictPicker[indexPath.row]!
cell.answerText.addTarget(self, action: #selector(AddFollowUpViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingDidEnd)
return cell
}
func textFieldDidChange(sender: UITextField){
let rowIndex: Int!
let selectValue = sender.tag
if let txtf = sender as? UITextField {
if let superview = txtf.superview {
if let cell = superview.superview as? AddFollowTableViewCell {
rowIndex = myTable.indexPathForCell(cell)?.row
dictAnswer[rowIndex] = selectValue - 1
}
}
}
}
After two days, it solved by thousands of trials:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell = myTable.dequeueReusableCellWithIdentifier("addFollowCell") as! AddFollowTableViewCell
if(cell.identifier == true){
cell.answerText.text = selectedAnswerForRow[indexPath.row]
}
cell.questionView.text = listQuestion1[indexPath.row]
cell.pickerDataSource = dictPicker[indexPath.row]!
dictAnswer[indexPath.row] = cell.pickerValue
cell.answerText.addTarget(self, action: #selector(AddFollowUpViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingDidEnd)
cell.identifier = true
return cell
}
func textFieldDidChange(sender: UITextField){
let rowIndex: Int!
let cell = sender.superview?.superview as! AddFollowTableViewCell
rowIndex = myTable.indexPathForCell(cell)?.row
selectedAnswerForRow[rowIndex] = cell.answerValue
print(selectedAnswerForRow[rowIndex])
cell.answerText.text = sender.text
cell.identifier = true
}
It might have some performance issue need to be optimised , but it shows exactly what i want. LOL
You're basically recycling your views and not clearing them. That's the whole point of -dequeueReusableCellWithIdentifier:indexPath:.
Allocating and deallocating memory is very power consuming, so the system recycles every cell that goes out of viewport bounds.
You don't set the text inside answerText (I assume it's the text field that causes trouble) so its content will be kept when recycled.
Assuming you'll store user selection inside a dictionary var selectedAnswerForRow: [IndexPath:String]:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = myTable.dequeueReusableCellWithIdentifier("addFollowCell", forIndexPath: indexPath) as! AddFollowTableViewCell
cell.questionView.text = listQuestion1[indexPath.row]
cell.pickerDataSource = dictPicker[indexPath.row]!
cell.answerText.addTarget(self, action: "textFieldDidChange:"), forControlEvents: UIControlEvents.EditingDidEnd)
cell.answerText.text = self.selectedAnswerForRow[indexPath] ?? "" // add this
return cell
}
self.selectedAnswerForRow[indexPath] ?? "" returns the result or an empty string if it's not present in the dictionary.
Also, you're adding several times the action for edition control event. You have to check first if it isn't already bound.
Because the cell is reused. So you have to implement prepareForReuse() in your custom cell class and reset all the changing variables
UPDATE
See :
class MyCell : UITableViewCell {
#IBOutlet weak var myTextField : UITextField!
//Add the following
override func prepareForReuse() {
myTextField.text = nil
myTextField.inputView = myPickerView
super.prepareForReuse()
}
}
I have a subclass of UITableViewCell that is shown in a TableView. Each cell has a text field. When the textFieldDidEndEditing func is called, I want to save the entered text as an attribute of an NSManagedObject in my Managed Object Context.
This function is implemented in my tableViewCell class:
func textFieldDidEndEditing(textField: UITextField) {
let viewController = ViewController()
let indexPath: NSIndexPath!
viewController.updateCommitsInMOC(self, atIndexPath: indexPath!)
}
And this is the function it calls. This function is implemented in my ViewController class, the one that controls the TableView which is made up of the tableViewCells:
func updateCommitsInMOC(cell: CommitTableViewCell, atIndexPath indexPath: NSIndexPath) {
// Fetch Commit
let commit = fetchedResultsController.objectAtIndexPath(indexPath) as! Commit
// Update Cell
commit.contents = cell.commitContents.text!
if cell.repeatStatus.selectedSegmentIndex == 1 { commit.repeatStatus = true }
saveManagedObjectContext()
}
I'm of course open to any suggestions as to other ways to implement the saving behavior every time the user is done editing the text field.
Is your question "How do I get the IndexPath"? Instead of the UITableviewCell trying to figure out what it's indexPath is in textFieldDidEndEditing, why don't you just figure it out within updateCommitsInMOC function?
Assuming you have a reference to your tableView you can just do this
func updateCommitsInMOC(cell: CommitTableViewCell) {
guard let indexPath = tableView.indexPathForCell(cell) else {
return
}
// Fetch Commit
let commit = fetchedResultsController.objectAtIndexPath(indexPath) as! Commit
// Update Cell
commit.contents = cell.commitContents.text!
if cell.repeatStatus.selectedSegmentIndex == 1 { commit.repeatStatus = true }
saveManagedObjectContext()
}
You can add a tag as row in cell textField.
like this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("idCell")
cell.textField.tag = indexPath.row
return cell
}
and the textField delegate:
func textFieldDidEndEditing(textField: UITextField) {
let viewController = ViewController()
let indexPath = NSIndexPath(forRow: textField.tag, section: 0)
viewController.updateCommitsInMOC(self, atIndexPath: indexPath!)
}
or you can use the superview:
func textFieldDidEndEditing(textField: UITextField) {
let view = textField.superview!
let cell = view.superview as! UITableViewCell
let viewController = ViewController()
let indexPath = itemTable.indexPathForCell(cell)
viewController.updateCommitsInMOC(self, atIndexPath: indexPath!)
}
I suggest you to use in your tableview the
setEditing(editing, animated: animated) method.
Then inside of it you can manage the single object retrieving it from the fetchResultController.indexPathForObject(inputObject) or as you used fetchedResultsController.objectAtIndexPath(indexPath).
Finally you can use self.managedObjectContext.saveToPersistentStore() or self.managedObjectContext.save().
When I tap a button in a custom cell and then scroll down (or up) another cell button is also tapped. I see that it's tapped because the button outlet that I created for the button is disabled.
My cellForRowAtIndexPath has a reuseIdentifier for the cell:
var cell: FeedTableViewCell? = tableView.dequeueReusableCellWithIdentifier("MusicCell") as? FeedTableViewCell
Considering I have the degueueReusableCellWithId in the cellForRowAtIndexPath do I need a prepareForReuse? When I add the prepareForReuse in my custom cell file, the cell just goes back to the default values (obviously because I reset it to the default values). Problem is I want it to keep the value of each indexPath.row.
This is how I'm querying the values:
override func queryForTable() -> PFQuery {
var query:PFQuery = PFQuery(className:"Music")
if(objects?.count == 0)
{
query.cachePolicy = PFCachePolicy.CacheThenNetwork
}
query.orderByAscending("videoId")
return query
}
This is the numberOfRowsInSection and cellForRowAtIndexPath
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects!.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var cell: FeedTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? FeedTableViewCell
if(cell == nil) {
cell = NSBundle.mainBundle().loadNibNamed("FeedTableViewCell", owner: self, options: nil)[0] as? FeedTableViewCell
}
if let pfObject = object {
//I took out the irrelevant methods. I can add them if that makes a difference...
var votes:Int? = pfObject["votes"] as? Int
if votes == nil {
votes = 0
}
cell?.votesLabel?.text = "\(votes!)"
}
I'm registering this in the viewDidLoad above the super.viewDidLoad()
tableView.registerNib(UINib(nibName: "FeedTableViewCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)
This is my button query in the customCell:
#IBAction func heartButton(sender: AnyObject) {
if(parseObject != nil) {
if var votes:Int? = parseObject!.objectForKey("votes") as? Int {
votes!++
parseObject!.setObject(votes!, forKey: "votes")
parseObject!.saveInBackground()
votesLabel?.text = "\(votes!)"
}
}
heartOutlet.enabled = false
}
Any help and suggestions mean a lot.
Thank you.
REFRENCE LINKS I USED:
I referred to several links but they were in objective-c and didn't help:
UICollectionView Tap Selects More Than One Cell
How to use prepareForReuse method
I also referred to the docs, and that didn't help much.
From the code you have posted, it is clear that you are not setting the enabled property of the UIButton with respect to the DataSource(The array and its objects you are using to load the tableview, that is the elements in objects array). Whatever objects that array contains, add a property to determine if the condition for the button should be true or false, and then in cellForRowAtIndexPath set the enabled property of the button according to that. When the button is clicked, add a callback to the ViewController(using a delegate) and set the property there.
Sample Code
In custom cell class:
protocol CellButtonDelegate
{
func buttonClicked(cell : PFTableViewCell)
}
public var delegate : CellButtonDelegate?
public var buttonEnabled : Bool?
{
get
{
return heartOutlet.enabled
}
set
{
heartOutlet.enabled = newValue
}
}
#IBAction func heartButton(sender: AnyObject) {
if(parseObject != nil) {
if var votes:Int? = parseObject!.objectForKey("votes") as? Int {
votes!++
parseObject!.setObject(votes!, forKey: "votes")
parseObject!.saveInBackground()
votesLabel?.text = "\(votes!)"
}
}
delegate?.buttonClicked(self)
}
In ViewController:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var cell: FeedTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? FeedTableViewCell
if(cell == nil) {
cell = NSBundle.mainBundle().loadNibNamed("FeedTableViewCell", owner: self, options: nil)[0] as? FeedTableViewCell
}
if let pfObject = object {
//I took out the irrelevant methods. I can add them if that makes a difference...
var votes:Int? = pfObject["votes"] as? Int
if votes == nil {
votes = 0
}
cell?.buttonEnabled = objects[indexPath.row].isEnabled //The new property you need to add. true by default
cell?.delegate = self //Make sure you implement the delgate
cell?.votesLabel?.text = "\(votes!)"
return cell?
}
func buttonClicked(cell : PFTableViewCell)
{
//Here, get the indexPath using the cell and assign the new property in the array.
}
Please note that the above code is rough. Just get the idea from the code and implement it as per your requirement.