I would like to create a view where this subview will be in repeated for as many times as there are element in an array
Fx
Array *labelText = [NSArray alloc]initWithObjects:#"Title1",#"Title2",nil];
2 elements in this array, so two subviews would be needed.
I will get the appropriate from the *labelText.
I don't know if it's possible, but i would be a foul not to ask the experts :)
It's very possible indeed. You might want to take a look at the excellent AQGridView
Related
Am a newbie to iOS programming. This is what am trying to do:
The user enters some text in the screen and it keeps getting added to a UITableView.
As usual, it's getting added from the top.
But I want to add it from the bottom i.e. each new message that's added is added above the rest/existing ones, and not below.
Can someone offer some pointer on this please!
Thanks
Priya
NSMutableArray's -addObject method appends the object to the end of the array. If you want to put it at the beginning, just use this method instead:
[inputArray insertObject:userInput atIndex:0];
There are other ways to put objects in the array and move them around. Take a look at the documentation:
NSMutableArray Documentation
Are you using CoreData? If so, maybe you can set a creation time and sort it descending?
If you don't use CoreData but store the data in some sort of array, just add new objects to the beginning of the array and then reload the data.
[array insertObject:data atIndex:0];
I want to create meachanical units convertor calculator in iphone sdk so i have to perform one to many type of operation. for example Length is category and there is multiple type of units in Length category for ex.meter,kilometer etc.Now for every unit i will have to create multiple combinations for that i'm using if-else conditions for now to work but practially this increases my code a lot because as there are almost 30 categories and each category has multiple units.So is there any another way to solve this problem in short way as it is too hectic to write so many if else combinations in my code. For this i thought that it might be possible to use two dimensional array.so please provide me code for two dimensional array to perform this calulation operation.
Just put NSArray objects in an NSArray and you have your 2 dimenensional array. (pretty much like in any other language.)
NSMutableArray * myTwoDimensionalArray = [NSMutableArray alloc]init];
[myTwoDimensionalArray addObject:[NSArray arrayWithObjects:#"value 0/0", #"value 0/1",nil]];
[myTwoDimensionalArray addObject:[NSArray arrayWithObjects:#"value 1/0", #"value 1/1",nil]];
// to get value at [i][j]
[[myTwoDimensionalArray objectAtIndex:i] objectAtIndex:j];
As jules has suggested you can use a NSMutableArray yo create your 2-d array. Another approach would be to have mXn number of objects in a single NSMUtableArray. Create a array and add objects sequentially. Access the [i][j] element by accessing the object at (i*n)+j.
I have question regarding about finding subview using '.tags' in one UIView.
for (UIView *viewObj in [self.view subviews])
{
// want to find viewObject by its tag.
// but I heard for-switch or for-case paradigm are bad pattern design by WTF.
// there are total 9 of tags will be use for finding view object
switch (viewObj.tag)
{
case 0:
.
..
...
}
}
How can I make this to be non-switch or non-if statement? Should I just add into one array then fetch from there? For example, you add views with desired tags and fetch from this array.
Thank you.
From your comments, it's a very finite set of items that will exist - 9 items. If that is expected to be static, there is nothing really wrong with a switch statement. Perhaps you can functionally decompose each switch into a separate method. That said, perhaps the command pattern would be one approach to consider, in addition to your array idea. For reference: http://en.wikipedia.org/wiki/Command_pattern
If it is only going to be a fixed number of views and each view has different methods that need to be performed you could access the views directly.
UIView* someView = [self.view viewWithTag:0];
//Operations on view 0
UIView* someOtherView = [self.view viewWithTag:1];
//Operations on view 1
//...
One thing you could do is have the UIViews in your subviews be of the same type by having each conform to the same protocol. Then you could cast each to id<YourProtocol> and call the same method within each UIView.
And to further Shan's answer, if you can decompose them into functions and put them (function pointers) into an array, then you may be able to make it a little simpler, or you can use the new "Blocks" language feature.
There's a new IBOutlet type, IBOutletCollection. You use that for an NSArray, and then in IB you can add views to that outlet - at runtime, they will all be put into the array (unordered).
To get out a specific one you want, in viewDidLoad you could map all of the NSArray entries into a dictionary keyed by tag value, then just use "objectForKey" to get them out.
I want to find a way how I can compare the actual hierarchical order of two subviews.
As i didn't find a method who turns out to give me this kind of result, I want to ask you. Im sure there must be a way to do this.
Is it possible, that the [view subviews] array is ordered with the same hierarchy?
Thanks,
Makrus
Although I have not find any reference about that in docs I'm pretty sure that the order of elements in subviews array corresponds to their z-order. You can try to change order of subviews in IB and log subviews to console - you will see that output supports that.
One more hint that it is really so are insertSubview:atIndex: and exchangeSubviewAtIndex:withSubviewAtIndex: methods which change subview's z-order and those changes are reflected in subviews array order...
I have a NSMutableArray that holds a collection of UIViewControllers
Amongst other properties and methods an instance of each of the viewControllers are placed in a parent view
The user will place these objects individually when desired to.
So essentially I use - (void)addSubview:(UIView *)view when a new view is added to the parent view controller.
Because the UI is isometric it's made things a tad more complicated
What I am trying to do is re-order the views based on their co-ordinate position, so items higher up the parent UIView frame is indexed lower then views lower in the parent UIview frame. And items that are on the left side of the view are positioned at a higher index to those on the right
I think the solution may have to do with re-ordering the NSMutableArray but how can I compare the CGpoints? Do I need to compare the x and y separately?
So users can drag views around inside a parent view? And when a view changes position, you want to change its place index among the subviews so that the draw order will be correct?
You can use -(void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2 do do this. It won't be enough to just reorder them in you array, since the parent UIView has its own array to keep track of the views. You should create a reorder method where you switch the views around both in your own array and in the parent view.
There are functions for checking equality of points, inclusion of points in rects, intersection of rects, etc, but here it just sounds like you want to get the origin.y point and use that to compare which views are further "back" or to the front...
As an aside, you might want to look at using CALayers, which have support for setting a z position as well.
This might be a simplistic answer, but maybe it's what you're looking for.
Assuming you have access to their CGpoints, you could write a...
-(NSComparisonResult) compare: (NSObject *) incoming;
method in every delegate that looked at the CGpoints of the incoming and "self" and returned a NSComparisonResult. Cool thing about this would be that you could just run...
[myMutableArray sortUsingSelector:#selector(compare:)];
anytime anything changed. You just have to setup the compare: method to return the right NSComparisonResult.
I've managed to resolve this, I did some reading on Key-Value Coding
I created a property called itemY to store the view.frame.origin.y value.
I created a method to re-assign the itemY with the current view.frame.origin.y value.
Now the ViewController that stores the array of ViewControllers uses a instance NSSortDescriptor with the property itemY.
A new NSArray is created using the NSSortDescriptor
Now I loop through the new NSArray and carry out my logic