Iterate from multi-dimension array in iOS - ios

How to iterate from this multi-dimensional array in iOS ?
This data is coming from JSON
(
(
"burger meal",
"getImage.ashx?id=1",
150,
300,
"get a free burger"
),
(
"chinese combo",
"getImage.ashx?id=2",
350,
700,
"get a combo free"
),
(
"cheeese cake",
"getImage.ashx?id=3",
350,
700,
"get a cake free with meal"
)
)
I need to use object at index 3 e.g. = 150,350,350 in UITableView .
I have tried this
NSArray *array = jsonArray;
for (NSArray *newArray in array) {
[newArray addObjectFromArray:array];
}

Try This, In this code arrResult contain all values have you required at particular position.
NSArray *array = jsonArray;
NSMutableArray *arrResult = [[NSMutableArray alloc]init];
for (NSArray *newArray in array) {
[arrResult addObject:[newArray objectAtIndex:2]];
}

This is simple. Let TableView iterate the arrays for you. Your tableView is already iterating, don't add to overhead by iterating over the array yourself. You will just waste processing power if you do.
First, modify your numberOfRowsInSection delegate method like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [jsonArray count]; //Provide your json array here.
}
Then you have to do this in your cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Do everything that you need to do to get the cell. Here we will deal with only string retrieval from json array.
if(jsonArray count]>indexPath.row){
NSArray *innerArray = [jsonArray objectAtIndex:indexPath.row]; //You got the inner array
if(innerArray count]>2){
NSString *yourDesiredString = [innerArray objectAtIndex:2]; //You got the string. Use it now as you wish. It will be 150 for the first cell, 350 for the second and 350 for the third one as well.
}
}
}
I have not added any exception checks here, that's what you have to do yourself. I have added basic checks to prevent crashes in case of irrelevant data, you should expand on that.
This is with the assumption that you wish to set this string to each cell. If I have misunderstood your question or you need to ask something, leave a comment

Related

Finding index of NSMutableDictionary inside an NSMutableArray

I have an NSMutableDictionary with a specific key and value. This dictionary is inside an NSMutableArray. I want to update the dictionary with a specific key which is at a certain index inside the array. I want to find the index of this dictionary object. Currently, my dictionary is at index 0 and my editingIndexPath is 1 which is of NSIndexPath type, so editingIndexPath.row doesn't help me.
My code is as follows:
//UpdatedDateAndTime is the NSMutableArray.
//selectedDay is a NSString object.
//dateKeySelected is also a string key.
[[updatedDateAndTime objectAtIndex:editingIndexPath.row] setObject:selectedDay forKey:dateKeySelected];
My problem is that I want to get the right index of the dictionary that is found.
The answer used to be a for loop with a counter, but you're in luck: NSArray has a new method, indexOfObject:, which should do the trick just fine:
NSUInteger i = [myArray indexOfObject:#{myKey:myValue}];
Swift: index(of:)
If your array contains just one special NSMutableDictionary then use below code. I didn't test it but the idea is to search NSMutableDictionary class in the array. And you have to do this search in indexPath which equals to the cell that you want to assign some data.
#interface ViewController (){
NSMutableArray *array;
NSMutableDictionary *dictionary;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (indexPath.row == 1) {
for (int i = 0; i < array.count; i++) {
if ([updatedDateAndTime[i] objectForKey:#"open_house_updated_date"] == selectedDay) {
NSLog(#"%i th index contains NSMutableDictionary that you want",i);
//do whatever needed
break;
}
}
}
else{
//define other cells which doesn't contain NSMutableDictionary
}
return cell;
}
#end
I hope this is what you are looking for.

Add a tag to NSArray object

I want to create 2 arrays, and populate my table view with that arrays. Then, if user click on a cell, that contain object from first array, i want to perform transition to my detail controller "one". Therefore, if user tap on cell, that contain text from second array, i want to perform segue for detail controller "two".
How to achieve that? I can't put tag on array objects and check it.
Edit: or, if NSDictionary suitable for that case, i could use them instead.
instead of having an array of data elements, you could have an array of MyDataClass, which has attributes for your data, and to identify the source.
You can use a single array to populate the table, and as suggested have different methods for populating the table cell based on the source.
Another way to do this would be create a third array with NSDictionary objects with two keys 'tag' and 'data'. 'tag' key will hold the information about which array and 'data' key will hold data from the array.
NSMutableArray *tableArray = [NSMutableArray arrayWithCapacity:0];
for (id obj in array1) {
[tableArray addObject:#{#"tag":#1, #"data":obj}];
}
for (id obj in array2) {
[tableArray addObject:#{#"tag":#2, #"data":obj}];
}
Then use this new array to populate your table
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
....
id obj = [[tableArray objectAtIndex:indexPath.row] objectForKey:#"data"];
....
}
and on didSelectRowAtIndexpath you can check value for 'tag' key to check whether it is from array1 or array2
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
....
NSNumber *tag = [[tableArray objectAtIndex:indexPath.row] objectForKey:#"tag"];
if (tag.intValue == 1) {
//controller 1
}
else if (tag.intValue == 2) {
//controller 2
}
....
}
There are multiple ways of looking to this problem. Firstly, you have to understand that you can populate a table view with only a single array; however this array can be made from multiple arrays.
Without getting ourselves dirty into multiple data structures that might provide a lot of redundancy than efficiency, a simple way would be to check for the array number in tableView:didSelectRowAtIndexPath:.
Example:
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
<Type> obj = tableViewArray[indexPath.row];
if (obj in array1)
{
// ...
}
else
{
// ...
}
}
The tableViewArray is probably array1+array2.

How to resize the UITableView row height in Objective C

In my app I have a requirement like saving the data in itemsArray in the following format like
iPhone
iPad
iPod Touch
and the same data I am tryin to display in the UITableView
but it is taking only the first item i.e iPhone
It is not displaying the remaining items in the itemsArray.
I have been trying this but I am not able to find the solution for it.
Can anyone please help me to solve this.
I am new to Objective C.
Thanks in Advance.
If you want all 3 items in single cell then create one dictionary as follows:
NSMutableArray * contentsArray = [NSMutableArray array];
NSMutableDictionary * allContacts = [NSMutableDictionary dictionary];
for(int i = 0; i < yourItemArrayCount; i++) {
[allContacts setObject:[yourItemArray objectAtIndex:i] forKey:#"key %#",[[youtItemArray objectAtIndex:i] stringValue]];
}
[contentsArray addObject:allContacts];
then in following method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Write your dictionary logic here
NSDictionary *dict = [contentsArray objectAtIndex:indexpath.row];
//Necessary logic will go here.
}
Add following method in your code
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60; //You can set height of cell here.
}
Debug your datasource in table datasource/delegates. Your problem is only for 3 items. It can be done statically as given in above blog.
But for actually resizing table view cell/row height. you will have to use autolayouts.
Follow the link:
//http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout

How to show first 50 name from an array?

My array contains more than 100 names and I just want to show the first 50 array.
I have read this topic (How do I get first x elements of an NSArray in Cocoa?) but is there a better solution instead of adding two arrays?
I'm adding the array like this
-(void)test{
_myArray = [[NSMutableArray alloc] init];
[_myArray addObject:usersName];
}
If you want to limit how much of the array can be seen within a tableview, where the array is the datasource of the tableview, then you just need to use the MIN macro with the tableview datasource
- tableView:numberOfRowsInSection: method:
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return MIN([_array count], 50);
}
The best way is to use subarrayWithRange:

Scrolling with indexbar in a tableview

I got a table view with approx 45k items. I want to use the indexbar to be able to scroll faster through my list.
Now I've looked at other questions on here, but I just can't figure out what to do.
I've got the following two functions:
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return alphabetsArray;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return index;
}
I don't understand what I should do next. Like, I have an array called airports which is sorted alphabetically. What should I be doing with this array?
update
so I found an article I could use and I'm pretty close now, I think. My table view only shows 2 items now though.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"ROWS METHOD CALLED");
NSArray *unsortedKeys = [alphabetizedAirports allKeys];
NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSString *key = [sortedKeys objectAtIndex:section];
NSArray *airportsForSection = [alphabetizedAirports objectForKey:key];
return [airportsForSection count];
}
I have about 60 items in my sortedKeys array. The airportsForSection array only gets 2 items though so when it returns the array, the tableview only shows 2 items. Any ideas how to solve?

Resources