adding a control to a custom cell view - ios

I am getting really frustrated trying to solve this problem i tried implementing it in many many ways but no solution. I have a UIStepper in a custom cell and i want to change a value on the cell. Everything works fine expect when i scroll around the tableView the values from the UIStepper changes from one cell to another. Please help here are my screen shoots.
Link to tableview implementation and link to cell implementation

You are trying to store the stepper value inside each individual cell. That's not going to work because cells are reused; the cell that you now see in row 2 may reappear in row 20 when the user scrolls.
That is why you must store the value for the stepper in the model (your data) on a row-by-row basis, so that you can set it freshly and correctly for that row every single time cellForRowAtIndexPath: is called.
This, in turn, means that as the user steps the stepper, its valueChanged is going to need to talk to the table view data source so that the model can be updated ("the stepper value for row 5 has just been changed to 3") and maintained in the model.

Related

Move CollectionViewCell to another section

I have a UIViewController with a collectionView inside it. The layout may seem confusing but I am making use of horizontal scrolling UICollectionViewCells, presented in 3 sections.
The main collection view is made up of 3 sections. I access 3 different UICollectionViewCell classes for each section becuase it is fetching different data for each one. Once the data is fetched, it dequeues another Cell class, which is the same one accessed for all the sections.
There are 2 buttons in each cell. When I press the button, a value changes within the database. When I re-run the program, the cell in which the button was pressed has moved to its corresponding section that it should be in as per the change. However, I want to achieve this as soon as I press the button. How can I move the cell from one section to another, baring in mind that they the cells presented are inside a collectionview cell making up the sections, which is then inside a greater UICollectionView.?
I've tried researching how to do this but no luck, and everyone seems to be using a drag and drop method which seems too complicated for what I am trying to achieve. Would appreciate any help.
Thanks
The approach I followed to achieve the above situation is using a custom Delegation when cell is removing from collectionviews. A container CollectionView has two sections with 2 different cells containing two different Horizontal UICollectionViews. These two collectionview's cells have UIButtons. When tap on one collectionView's button, that particular cell is removed, custom Delegate method calls and another CollectionView's cell is populated. This is the link of the project I have created for this particular scenario. For better understanding, here is a GIF for demonstration.

Getting the data of all TableView Cell which all has TextFields in it (including the hidden views)

I'd like to get every data that is within all cells in one tableview which is quite a long list.
I'm looking for an approach on how to retrieve everything including those hidden in view, which I know the views are reused. I think some of you might have experienced this problem before, what are your approach on this?
I've tried
let cells = self.tableView.visibleCells
then looping into every cell and saving each data to an array but it is not effective in getting those that aren't part of the view or hidden. Is there a way to get over this?
In cellForRowAtIndexPath, YOU are telling the table what is in each cell. So why would you turn around and ask the table what's in each cell? If the user puts "Hello" in your first cell, then scrolls the table enough to push that first cell out of view, then when the user scrolls back to the top, YOU are the one telling it to put "Hello" back in that first cell. YOU own the data source, not the table.
You need a data source. That can be "empty" at first, maybe an array of empty strings if that's what you want (each index in the array could map to a table row for example). But then, as the user interacts with the text fields in the cells, you need to update that data source with the text they entered.
You should use that data source as your source for the cellForRowAtIndex method. That way you can handle populating the cells when they are requested by the table, and you also know all the data when the user is done.
Why not just update the model each time the user taps a key when editing a textfield? You could create a protocol for that cell subclass and make your view controller the delegate for each cell. As long as cells are guaranteed to stay on the screen while you're typing (you'll get some weird behaviors if not) the cell can send a message to the view controller or whatever you hook it up to telling it what new value to store. Then everything is already stored for you when you need the full list, and you don't have to interact with the tableview.

Information from a PickerView appears in another line of the TableView. How to solve?

Hello programmers friends. I have a situation I can not explain. I'll leave the prints on the screens and then describe what happened.
-
-
[-][3]
Initially I select the quantity in the pickerview and it changes in the UITextField field. Then when I scroll the screen, the 6 row of the table is with the PickerView information from row 1, and then when I roll the screen up, the data is lost. Has anyone ever had a similar problem?
When a quantity is selected, you need to persist that information in your data model. UITableView only creates enough rows to show what's visible on screen and it re-creates rows as they are scrolled into view. So you need to have the quantity information stored elsewhere to provide to the table view when it asks again for the row information. Hope this makes sense?
As Paulw11 mentioned, cells within a UITableView or a UICollectionView are reused, thus if you are manually creating the UIPickerView within the cell itself you have two options:
1) Rather than create the UIPickerView via code, subclass the UICollectionViewCell being used and add the UIPickerView either in the Xib / Storyboard(where ever you created the cell view).
2) In the prepareForReuse() function within a cell, check if the UIPickerView variable is not nil, and if it is not nil, then remove it and set it to nil.

deleteRowsAtIndexPaths resets custom cells

I'm relatively new to Swift and iOS and I have one issue - I have a custom SwipeCell class that extends UITableViewCell. It's a cell that can be swiped left and right, and has buttons on each side. For some reason if I made the buttons out of the frame of the cell when user swipes the cell they would appear but could not call an action, so my solution was to make cell wider than it is (so buttons can fit in it). Because it was done this way my cell has to have an offset by default for the total width of the buttons of the left (let's say 100), so it's position is X:-100.
And that's fine, and everything works fines with the cells, however there is one huge issue - if I call the deletion of any cell from the tableView like this
tableView.deleteRowsAtIndexPaths([tableView.indexPathForCell(activeCell)!], withRowAnimation: .None)
tableView then deletes the cell, and all of the other cells that are currently visible (doesn't happen with cells above or below the screen bounds) get moved to X:0 instead of staying at X:-100, so I assume that deleteRowsAtIndexPaths calls some function that resets the visible cells positions to 0,0. I'm currently setting swipe cells positions with layoutSubviews() since the number of buttons is dynamic and couldn't be determined upfront, but layoutSubviews is not the function where the bug happens.
So to sum up question - what function does deleteRowsAtIndexPaths call after deleting a cell that resets/redraws the visible cells?
deleteRowsAtIndexPaths deletes a row(s) from the tableView. This is handled internally by iOS. From Apple documentation:
Deletes the rows specified by an array of index paths, with an option
to animate the deletion.
Another interesting thing to note here is that this method does not modify your model (object that holds data used by your table view cells to render on themselves). You have to do that yourself. The cells are deleted, but if you call reloadData without deleting the row from your Model, cell will reappear.
Expect that cells get deleted and created all the time. Cells are very, very temporary objects. Write the code to create one when needed, and don't make any assumptions. Don't assume the cell is there later, don't assume it's in the same row, don't assume it displays the same data (because cells are recycled).
Since I could find out what happens inside of deleteRowAtIndexPaths, and why it changes frames of each Cell, I decided to just do my own function that deletes a row, by obtaining cells with tableView.visibleCells, and then just moving cells bellow the deleted cell up by a height of deleted cell. Thank you all for trying to help me, and especially thanks to Abhinav who told me that it was handled internally, which help me decide to write a custom code for the deletion.

how to get all text from uitextfield in uitableview

i have a table view with a UITextField in each cell.the table has 30 lines。
i want to get all values from 30 UITextfield,But it works fine for 10 visible cells. I cant't access any invisible cells. the text returns null. please help me.I am a chese developer
I cant't access any invisible cells. the text returns null.
That's how it is supposed to work since UITableView optimizes its use of cells and only displayed cells are actually existing at any time. (at least, if you are using dequeueReusableCellWithIdentifier: as you will find in most UITableView tutorials).
You should possibly store the text entered by the user in each table row somewhere (in your model?) and then access it from there.
Alternatively, you could avoid using dequeueReusableCellWithIdentifier: but in that case you should manage the set of cells in the table view on your own. (I am not encouraging your to do this, it's just a possibility).

Resources