i am looking for the best solution create a TableView with different Layouts.
For Example: I need a few rows, with with a Label and a Textview, and some with a Label and a Textbox.
At the Moment i have 2 Custom Cells, and create an instance of them in my TableView function, and return the correct cell depending on my indexPath.row (this is also the key in my Array where i cant find out with type of Layout i need).
Is this the correct way do handle this?
Another Question is how to get these Values from the Textbox. Should i use the "AfterEdid" Action from TableView/Textbox and Write them into one global Array? Or is it possible to retrieve the Values easer from a Textbox/Testview inside a cell in a Table?
Thanks in Advance
The correct way is to define two cell prototypes in the Storyboard, each one with a different cell identifier. The in the UITableViewDataSource, method tableView: cellForRowAtIndexPath, you should dequeue the cell of correct type based on your application logic.
Related
I'm trying to set up a view-based table in Swift using bindings. All of the examples I've seen use a datasource/delegate setup.
I have an array of Flag objects which has two properties - flagName: String and flagImage: NSImage. I have an NSArrayController managing this array.
If I set up a cell-based table, and bind one column to arrangedObjects.flagImage and the other to arrangedObjects.flagName, I get a table displaying images and names, and I can use the array controller's add and remove methods, so there are no problems with my datasource or my array controller.
I have been following the instructions in Apple's TableView Programming Guide to bind my view-based table to my array controller:
tableView Content binding: FlagController.arrangedObjects
textField Value binding: TableCellView.objectValue.flagName
imageView Value binding: TableCellView.objectValue.flagImage
(IBs autocomplete is not happy with the paths for objectValue.flagName respectively flagImage; it doesn't feel that there should be any completion whatsoever and says it can't resolve the path, so it looks as if the problem is with the tableView's content.)
If I do this, my table has a number of rows that corresponds to the number of elements that my array controller is managing at that moment (I have two simple setups, one object vs. 50 objects, so it's clear that something is bound). What I don't get is a display; and selecting a table row does not seem to send back a message to my flagController.
What am I missing? Has anyone been able to make this work? I've had no problems with other bindings in Swift so far, but I'm starting to think that the sudden reappearance of datasource examples is not unrelated to this.
It sounds like you've failed to implement the delegate method -tableView:viewForTableColumn:row:. That's a common cause of blank tables.
You don't actually have to implement that if you make sure the identifier of the table column and the identifier of the table cell view are the same in IB.
Otherwise, the method can just do this:
- (NSView*) tableView:(NSTableView*)tableView viewForTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row
{
return [tableView makeViewWithIdentifier:#"TheIdentifierOfYourTableCellView" owner:self];
}
(It could also do more, if desired.)
I have a number of custom cell objects (subclasses of UITableViewCell) with a couple of values in them to allow for user interaction within individual cells (like steppers or something). These values are stored within the custom cell class, since calling up to the owner of the table view seemed like a bad idea at the time.
I know of the function (NSArray *) visibleCells. Will that allow me to access the data within the cell objects?
If not, how?
I'm assuming that I can use the built-in functions of the UITableView to pull returned UITableViewCells, but is that sufficent when I'm talking about a subclass of that called, say CustomizerCell?
The function:
- (NSArray *)indexPathsForVisibleRows
Answers an array of index paths. Those index paths can be used the same way your cellForRowAtIndexPath uses the passed index path to access your model.
MyObject *myCustomDataSupportingACell = [myDatasourceArray objectAtIndex:indexPath.row];
I would like for each of the cells in UITableView be an entry field. In other words when the table appears and a user clicks/taps on a specific cell, the keyboard will appear and what ever the user types gets enter into the field. I will later use all these field as data in an array, for other uses.
Add a UITextField to each cell (design a .xib file for the cells and programmatically load the .xib as part of your table-view implementation). Implement UITextFieldDelegate, probably the textFieldShouldEndEditing: method because you might want to validate the input first and not let editing finish until the input is valid. In that method, if input is valid then store it in the array corresponding to what you were talking about. You'll need to do some tricks to find out what row you're currently validating, there's several ways to do it (one of which is shown here).
Hi Guys I have a problem!
I want to change the custom cell if mediaurl==#"string" and move the label on the left if i not load the picture... How can I do? Any issue? if I create a label from code the the method call if mediaurl==#"string" I will make a mistake.
I think this might be related to your other question
iPhone Problem with custom cell
What you want to do is use two different cell types, correct? If so, you should define two different cell identifiers (eg: #"NormalCell" & #"CustomCell") and dequeue only the kind of cell you need for displaying the current item.
Instead of modifying the cell to suit the data, choose what kind of cell to use depending on what you need to display.
To do this, you need to move your check for string equality (or whatever you use to determine cell type) to before you dequeue a reusable cell.
I have a UITableView with style "Grouped" which I use to set some options in my App. I'd like for one of the cells of this UITableView to only show up depending on whether another of this UITableView's cells is activated or not. If it's not, the first cell should show up (preferably with a smooth animation), if it is, the first cell should hide.
I tried returning nil in the appropriate -tableView:cellForRowAtIndexPath: to hide the cell, but that doesn't work and instead throws an exception.
I'm currently stuck and out of ideas how to solve this, so I hope some of you can point me in the right direction.
You should remove the data behind the hidden cells from the table view's data source.
For example, if you are using an array, when an action occurs that causes a cell to be hidden, you would remove the object for that row from the array. Then, as the table view's data source, the array will return one less total count and only return valid cells for every row in that count (no nil).
This approach may require maintaining a second array with all of the objects (including hidden).
To update the view, check out reloadRowsAtIndexPaths:withRowAnimation:.
Here's a handy post in which the author provides some source code for performing animations on the currently selected cell:
http://iphonedevelopment.blogspot.com/2010/01/navigation-based-core-data-application.html
He's using this in a NSFetchedResultsController context, but you can see how he's using various calls to add/remove cells & sections.
Now, in your case, you'll need to modify whatever array you're using to host the data used to generate the rows in your tableView when you "activate" your cell, then selectively use:
tableView:insertRowsAtIndexPaths:withRowAnimation:
tableView:deleteRowsAtIndexPaths:withRowAnimation:
tableView:insertSections:withRowAnimation:
tableView:deleteSections:withRowAnimation:
to adjust things accordingly (you can start with tableView:reloadData:, but it's inefficient).
I realize that the API can be a bit daunting, but take the time to read through it and understand what the various calls do. Understanding how the UITableView uses its datasource and delegate, as well as the chain of events that occur when cells are selected/deleted/etc., is important if you want to get things just right (and crash-free).
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:withRowAnimation:]; // or insertRowsAtIndexPaths:withAnimation:
[tableView endUpdates];
Before cellForRowAtIndexPath is called, numberOfRowsInSection is called. You should return the appropriate value of cells in the section there, so if you only want to show 1 cell, return one. The logic what cells are shown has to be implemented partially in both methods