layoutAttributesForItemAtIndexPath - when is it called? - ios6

I am currently playing around with some demo projects for the new UICollectionView.
Most of this demos override layoutAttributesForItemAtIndexPath in there subclasses. But the method is never called in one of that example.
From the Apple docs i know that i have to override layoutAttributesForItemAtIndexPath
I cannot not figure out in which situation this method is called.
Is this method just for special cases?

Maybe you can find out in Apple doc, Subclasses must override both layoutAttributesForElementsInRect and layoutAttributesForItemAtIndexPath
read this:
layoutAttributesForElementsInRect:
Returns the layout attributes for all of the cells and views in the specified rectangle.
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
Parameters
rect
The rectangle (specified in the collection view’s coordinate system) containing the target views.
Return Value
An array of UICollectionViewLayoutAttributes objects representing the layout information for the cells and views. The default implementation returns nil.
Discussion
Subclasses must override this method and use it to return layout information for all items whose view intersects the specified rectangle. Your implementation should return attributes for all visual elements, including cells, supplementary views, and decoration views.
When creating the layout attributes, always create an attributes object that represents the correct element type (cell, supplementary, or decoration). The collection view differentiates between attributes for each type and uses that information to make decisions about which views to create and how to manage them.
Availability
Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

Related

WPF two panel app - reuse or create new instance of panel VM?

I have a WPF application whose main window contains two panels. The layout is pretty standard. The left panel is a a list of objects. The right panel displays information about the object selected in the left panel.
Right now I have the left panel directly in the XAML of the window, although I'm thinking it should be its own UC/View. The right panel is coded as a UC. The ViewModel for the main window has a field that is the VM for the right panel (call it the properties VM). The properties VM encapsulates the data for the selected object from the model.
When a different object is selected in the left panel I see two options:
1) Instantiate a new instance of the properties VM and load the corresponding data from the model
2) Load the corresponding data from the model to the existing properties VM, and issue appropriate change notifications [My current implementation]
Is there an advantage or preferred way of doing things between the two options above? Am I missing a better third option?
* Edit * Maybe a better question is - what are the advantages and disadvantages of the two methods?
Thanks!
You are describing a very standard scenario in the MVVM world. I believe you have a slight architectural issue that when resolved would make this question moot. The 'list' from where you are making your selection should already be a list of view models that wrap the 'object' a.k.a model. The properties view should simply be databound to the selected VM.
Is there an advantage or preferred way of doing things between the two options above?
Not really, but it's probably simpler to just create a new instance and set the data-bound property to this instance instead of trying to re-use and update the state of an existing instance each time the button is clicked.
If you need to cache instances, you could use an IoC container or a custom cache container that resolves the instances for you. You could for example set up the container to always return the same instance of the same class or return a new instance each time depending on your requirements.
But there is "preferred way" really. Your requirements decide what would be the best way to solve your specific issue or use case.

MVVM - dynamically create multiple instances of view/viewmodel

I have a requirement to display a number of graph/chart "thumbnails". Clicking one will show an "expanded view" (in a separate panel) that displays a larger version of the chart, plus controls to view and manipulate the chart.
There will be a number of different charts, each plotting my data in different ways. Also, a given chart type may appear several times, each plotting a different subset of the data. The controls in the "expanded" view will also differ from one chart type to the next, so there is little commonality here.
I'm struggling to get my head around how to model all this in MVVM, especially given the need to dynamically create an unknown number of thumbnails (and in some cases multiple instances of the same type).
Thinking aloud, I guess I need a view/viewmodel that represents a single thumbnail (the view contains the chart component and the VM exposes the data to plot). I guess I also need a V/VM for the "thumbnail list" UI, responsible for creating the thumbnails and exposing them via a collection for binding to the list. But how does it instantiate these? A VM gets injected into its view, suggesting the "thumbnail list" VM would have to dynamically instantiate the thumbnail views - but a VM shouldn't have knowledge of views should it?!
Lastly, when I display the "expanded" view, it would make sense to (somehow) pass it the charting component/view that was used in the thumbnail, to avoid having to render the chart again, but how?
If it's relevant/helps, I'm using Castle Windsor for dependency injection, and the navigation features of Prism.
This is indeed a complex topic,...
I would suggest a VM for the list of icons not necessarily for the icon itself. this can be bound to properities of the IconListViewModels. Then you should think about a ChartViewModelFactory. Which works in conjunction with your DIC.
An important discussion is the VM-V marriage. View first or View Model first... one way could also be ViewResolver if which returns the matching view based on your view model... this can rely on some sort of conventions. So the final steps could be ask the factory for a view model find the matching view glue them together and bind them to a content presenter...
I hope this helps to get you started...

DataGridView with DataSource inheritance

I'm trying to create an extended version of the WinForms DataGridView (ElementDataGrid) to allow sorting and filtering. Since this will be a widget used by multiple developers, I want to hide the SortableBindingList class internally and have the user pass in just a normal List with the control creating the SortableBindingList.
I created a base class called Element, which other developers can extend, but when I set the DataPropertyName of a column to a property that isn't in Element, nothing shows up in that column. As an example, I have a Comment class that inherits from Element. I want to display the Comment Date and Comment Text in the datagrid. Neither of those columns have any data in them, but the columns using properties inherited from Element display properly.
Is there a straightforward way to have the grid display property values from classes that inherit from the Element base class? Alternately, is there a way I could have the property take a generic List?
UPDATE: Here's the method I'm using to set the data source to my SortableBindingList. As I said, properties from Element are being populated in the grid when I want to show them, but properties from Comment, which inherits from Element are not.
public List<Element> DataElements
{
set
{
bindingDataSource.Clear();
SortableBindingList<Element> boundDataSource = new SortableBindingList<Element>();
bindingDataSource.DataSource = boundDataSource;
foreach (Element e in value)
{
bindingDataSource.Add(e);
}
this.DataSource = bindingDataSource;
}
}
Take a look at Marc Gravell's answer to this SO question. Assuming, as he talks about in his answer, that the data is homogeneous (meaning that you aren't mixing Comments and SomeOtherClass in your List of Element) and has at least a single element in it (so that it is able to infer the actual type of the data in the list), I think it would work for your situation.

In Gtk, is it possible to have one cell renderer per treeview row?

In Gtk+ it is possible to have several cell renderers per column, however I want to have different cell renderers in different rows. For example, I'd like to list the properties of an object in a tree view. For boolean properties a toggle button would be much simpler to use than editing the GtkCellRendererText and type in TRUE or FALSE.
I know that I can react to the row data by setting a callback via gtk_tree_view_column_set_cell_data_func(). But in the callback, the cell renderer is already fixed and I can only change its appearance.
Edit: ptomato pointed to how it could be done in Vala. I took this information and build a C-based cell renderer that takes a GObject and a list store (assuming the first column to contain property names) to show a custom cell depending on the type of the property. I also subclassed a tree view widget that combines this for easier usage. Both components can be find at Github.
Here's how dconf-editor does it: subclass GtkCellRenderer to make a custom renderer, make a property called renderer that returns a GtkCellRendererText, GtkCellRendererToggle, etc. depending on what is needed, and override all the GtkCellRenderer signals such as get_size, render, activate, etc. to pass them along to the underlying renderer.
It's done in Vala but it shouldn't be too hard to convert to C.

Bring control to view

Is there an easy/good way to bring control into view (make it visible on screen) ?
So far I have tried to do it manually by walking component trees and invoking relevant methods (BringIntoView) and properties (Visibility) but it is rather complicated due to multiplicity of base types (FrameworkElement vs FrameworkContentElement, there is also Visual3D and maybe more?), parent relationships (visual, logical, template) and necessity of customized handling of certain types (e.g. TabItem.IsSelected, Expander.IsExpanded - and ideally that should not assume that control is inside Content). There are probably many edge cases that are not covered by this.
UPDATE
The code I'm using currently: https://gist.github.com/2761622

Resources