Access custom cell within - (void)tableView:commitEditingStyle:forRowAtIndexPath: - iphone

does anybody know how i can access a custom cell within the - (void)tableView:commitEditingStyle:forRowAtIndexPath:. I need access to my Custom Cell Class to retrieve some parameters.
Thanks.

There are at least two ways to do this. The index path you're getting in the commitEditing callback is the same as the one for the relevant cell. So, you could call your own UITableView delegate method to retrieve the cell at the given index path and cast it to the desired type. However, it's generally undesirable to use views (like cells) to store model data. Instead, you can store the necessary data in whatever data source you're using to build the cell in the first place, then index into it using the section/row combination of the provided index path.

Related

auto create stackView from array count

Please tell me if I chose the right approach or are there other options?
I receive an array of objects from the server.
According to the task I have to place each object of the array in the table cell
How many objects will come I don’t know, I just mean that they all have the same structure and that they should be placed automatically.
I chose the path - stack view
Is there any easy way to automatically generate their number?
And whether I think correctly...
I need to create 1n total stack in the cell and transfer new stack to inside
there is response from server
https://jsoneditoronline.org/?id=9ebed4100d4d4db49aab728a16ac693d
each array must be a cell
inside each array - the array is a data set that should be automatically insert ....
maybe in the stack?
Use UITableView. It will be better choise
upd:
Use complex UITableView with sections.
Also some like this can help you: https://medium.com/#stasost/ios-how-to-build-a-table-view-with-multiple-cell-types-2df91a206429

How can I store a cell height value in a Realm object that is associated with the cell?

I am using Realm Swift on iOS to store objects associated with UITableViewCell. Each cell is of a dynamic height, and I would like to store that height information in the Realm object as well.
But unfortunately, I cannot put the code to update the cell height information in the notification block of the Realm object since it will trigger an infinite loop. I tried adding it to another class which creates some sort of foreign key relationship with the base object and that somehow triggered an infinite loop too as I try to update the other object in the notification block.
What would be the best way out for this problem?
Much like Cocoa's Key Value Observing, Realm's notifications indicate that an object was modified if you set any properties on the object, even if you set the properties to their existing values. This is likely what is triggering your notification loop. To avoid this, you can check within your notification block whether you're setting your cell height property to the same value that the object already has. If so, skip updating the property.

Dynamically declaring number of buttons in a for loop in Swift

Is there a way to dynamically declare a certain number of UIButtons based on the number of iterations in a for loop?
the actual number would be passed in from the user or based on an array length
so pseudo-code would be
for num in total{
//declare a UIbutton with a unique name
}
If you are using UITableView the best way to accomplish this is using the table own behavior to populate its cells automatically with your source date (array, dictionaries, etc) even with data gathered from external sources like a REST service.
The way to do this is creating a custom cell with each outlet and point them to your sources.
After some googling, I think this is called metaprogramming and that swift doesn't have it yet :(
edit: looks like i was way overthinking things. this is actually fairly straightforward if i think about it without looping

UIViewController Design Pattern multiple UITableViews

I am having a view to display a list of Shops within a UITableView.
The user should have the possibility to modify the shops displayed by some filters.
For example it should be possible to display the shops ordered by name (with the first letter as section header). Further the user should be able to display just the favourites or sort the shops by category (category name is section header):
I did not want one table view, handling all the filters because I thought it would be a mess of if-then-else tags depending on the current filter. So I decided to create 4
UITableViewControllers with each of them handling one filter.
That's exactly what I need, no redundancy so far, because the tableVC delegate and datasource methods are the ones which need to be implemented differently.
But now I do need a header which appears depending on the scroll position of the table. This is exactly the same in all of the 4 tables. But how should I solve this issue now?
Implement the UIScrollViewDelegate methods in each of the four UITableViewControllers redundant? Create a ParentViewController handling the scrollView methods (which would not be so easy because of the table datasource and delegate methods...
Any suggestions how to solve this properly? How would you do this?
I would appreciate any help, thanks in advance.
I would create a parent class, taking care of the header and derive the UITableViewControllers for the filters from it.
The superclass takes care of the common elements (like the header), the subclasses take care of the actual display.
Alternatively, you can create an overloaded method, which returns an Array of cells to display.
Store this array and when the delegate (in the superclass) asks for the 'cell data', give him the corresponding element in the array.

Pattern to update a UItableView and its datasource after filtering with NSPredicate

This seems like it should be straight forward. Technically I can make this work, but if I want to perform certain UI animations, (using deleteRowsAtIndexPaths:withRowAnimation:) things get hairy. It seems as though the datasource and UI are fighting over who should delete first. Then I have to artificially keep data around for the UI update.
My current setup:
In my model:
Create a multidimensional NSArray to
hold my data. (each sub array
represents a section).
Place a NSDictionary in each section
array to represent the data for an
individual cell.
In my View Controller:
Create a multidimensional NSArray to
hold my Cell Controllers (mimicking
the data structure).
Assign each model dictionary to an
ivar in the appropriate Cell
Controller.
Hook up the data to the cell
This gets my UITableView on screen with cells properly displayed. Now if I want to filter the data in the table I do the following:
In my model:
Create an instance of NSPredicate.
Filter each subarray of the
multidimensional array.
Set a flag in each dictionary that
is within the results of the
filtered array.
In my View controller:
I create a new Multidimensional
NSArray. This array will hold the
cell controllers of the "filtered
data"
Loop through the current
Multidimensional Controller array.
Add it to the new "filtered array if
it's model's flag is set.
Replace the unfiltered Controller
array with the new one.
Update the table.
This also works very well. As long as I don't mind the UI updating instantaneously. If I want to delete rows with animation, I must pass those indexes to the table to be deleted.
To accomplish this, I add a few more steps to the view controller:
In my View controller:
I create a new Multidimensional
NSArray. This array will hold the
cell controllers of the "filtered
data"
Loop through the current
Multidimensional Controller array.
Add it to the new "filtered array if
it's model's flag is set.
Create a new NSArray to hold indexes
to be deleted.
Loop through the current
Multidimensional Controller array.
Add its cell's index to the
"toBeDeleted" array if its model's
flag is NOT set.
Replace the unfiltered Controller
array with the new one.
perform
deleteRowsAtIndexPaths:withRowAnimationn.
So far this method doesn't work the greatest. I am having issues deleting the rows with edge cases (empty sections and all visible cells being deleted).
This made me think maybe my methodology is flawed. Has anyone implemented this in an app? How did you solve this? Do your row animations work?
Not to make matters worse, but have you tried NSPredicate on an actual device?
I found that NSPredicate worked on the Simulator, but much like NSXMLDocument, wasn't available on the device itself (2.2.1).
update: Given that NSPredicate is available, all I can add is that I've had unpredictable results with deleteRowsAtIndexPaths:withRowAnimation as well - I had to settle for calling reloadData and having a non-animated transition.
Fortunately, since I have a highly "subtractive" filter in my app, there's typically a lot of movement, so it's obvious to the user that something has happened.
In your case where visible changes are potentially non-obvious, I suspect that from a usability perspective, it might be sufficient simply to use an animated UIImageView moving from north to south across the UITableView area. (a challenge in it's own right, unfortunately).
This would have the added bonus of confirming the filter's application to the user even if no visible changes occurred.