Array concatenation - iphone

I have a table view with 3 rows, if I select the first row , the item on the first row get saved in a array variable say arrdata , similarly when I select 2nd and 3rd row , I want it to get saved in the same variable arrData.

You can use NSMutableArray.
For Ex.
NSMutableArray *arrData = [[NSMutableArray alloc] init];
Now, select the first row from table view
Use [arrData addObject:[first row data]];
For 2nd from table view
Use [arrData addObject:[2nd row data]];
For 3rd from table view
Use [arrData addObject:[3rd row data]];
And you can also add some condition for add data in to arrData, like if 1st row data is added then do't re-add it.
For Ex. When you select 1st row you can add 1, 2nd selection add 2, 3rd selection add 3 like [arrData addObject:#"1"]; etc.
Now, If you want to get data from arrData,
If (arrData.count > 0)
{
NSString *fData = [arrData objectAtIndex:0]; // Here you get stored data from 1st selected row of table.
}

Create an nsmutable array and add objects by using method addobject
NSMutableArray *arrData =[[NSMutableArray alloc]ini];
On Tableselection method , add the following line
[arrdata addObject:your object];

Related

How to Display this kind of data into UITableview

I am having NSArray in which it is having following data, i am using AFHTTPRequestOperation which give me result and after that i am doing
[ListOfName addObject:[responseData valueForKey:name]]; and getting following result and that result i want to display in tableview but can understand how to do it becuause i an new to iphone
(
(
"Richard Conover",
"Richard Conover",
"Kaitlyn Matheson",
"Andrea Wannemaker",
"Andrea Wannemaker",
test,
james,
test,
gaurav,
sdfsdfs
)
)
if i do NSArray.count it will return only 1 so how to print it separately in tableview
What you want to do is set the TableView datasource to be the first object in that Array (which is another Array). Something like this:
NSArray *myTableViewDataSourceArray = [myOriginalArray objectAtIndex:0];
Then use myTableViewDataSourceArray for the datasource methods of the TableView.
As per your structure, you having array with objects which also array.
That means,[ListOfName addObject:[responseData valueForKey:name]]; will add array object. You try to load this array of data into your table view. So you can try this in your tableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
{
return [[ListOfName objectAtIndex:0] count];
}
It's a Nested Array (means Array inside Array).
Howmany Nested Arrays you have ?
If it's 1 then you can use :
NSArray *finalArray = [[NSArray alloc] initWithArray:[yourArray objectAtIndex:0]];
then you can use finalArray to populate the UITableView.

How do I add objects to an array that is inside an array?

I am trying to add an object to an array that is inside an array.
Here is my storyboard. Screen A is a simple tableView containing an array with object A, Screen B adds new objects to screen A. Each object A contains an array with detail (object B), these details are shown in screen C and you add details to object A in screen D.
So my model is as you can see above. I got Array A containing object A, each object contains Array B containing object B. Both my arrays are Mutable.
Object A = budget
Object B = item
I can not figure out how to add object B to array B.
- (void)addItemViewController:(AddItemViewController *)controller didFinishAddingItem:(Item *)item
int newRowIndex = [self.budgets.items count];
[self.dataModel.budgetsList addObjectFromArray:item];
NSLog(#"Item with name %# added", item.name);
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
[self dismissViewControllerAnimated:YES completion:nil];
This is what I am doing so fare. My problem here is that I am adding item (object B) into budget array (array A). :/
Thanks in advance.
When you are doing this:
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
You are mixing Presentation with data. What you need here is get the object element (the array in your case) that this indexpath corresponds to. As per the Table view design pattern, every table view reads its cells from an underlying collection of data objects. Have you defined this object (most preferably in separate objective-c .m and .h files)?
As for adding array into another array, NSArray just expects NSObject as element, so it is pretty straight-forward to add one into another.
NSArray *arrayB = [[NSArray alloc] init]; //any other initialization is good as well
NSArray *arrayA= [NSArray arrayWithObject:arrayB];
The above code is valid for any pair of NSArrays in your code.
If you want to add an object to Array B, then Use:
[[[array A objectAtIndex:indexPath] arrayB] addObject:yourObject];
Or you can use (this is an expansion of above code):
ObjectA *temp = [array A objectAtIndex:indexPath];
NSMutableArray *tempArray = [temp arrayB];
[tempArray addObject:yourObject];
Cast your object B to Item then do
[self.dataModel.budgetList replaceObjectAtIndex:11 withObject:(Item)item];
This code assume that you want to replace the existing object inside A and the the index is 11. If you want to add you just use insertObjectAtIndex: withObject:

UITableView checkmark count

I am using an UITableView with checkmarks and I want to set a label count which shows how many items are checkmarked in the table view. For example if the table contains 18 items with 2 checks,it shows 2/18. Can any one help me to code?
Assuming that this means that these cells will be selected, you could try this:
NSArray *array = [[NSArray alloc] initWithArray:tableView.indexPathsForSelectedRows];
myCount = [array count];
alternatively, you could keep a running tally of an integer and add to it or subtract from it every time a cell is selected or deselected.

iphone:Error while fetching data from array of one view to another view

In my apps i have two simple view. in first view i show only static values through array into table now by clicking on the row the view ll change an the value of that row will show in the second view. In my apps values are stored correctly but when i code in second view to display that value it does not show any error but value is not display..My code is below:
-(void)viewWillAppear:(BOOL)Animated{
txtName.text=data;
}
data is NSString in that i stored the values of an array.
Following will work...
txtName.text = [NSString stringWithFormat:#"%#",[data description]];

Dynamic initialization of controls in iphone

I am tying to add the values from an array to a number of text boxes in my view. it is possible for me to make it work by implementing
a1.text=[arrayName objectAtIndex:1];
Similarly i am having a1, a2, a3,... a45,... a60,... upto a99
Here "a1" is the outlet of a particular textfield. what i need is that, i have a plenty of controls on my screen. so its not possible for me to add each and every line. is there any way to add the values from the array to the corresponding textboxes.
what i need is to have a loop which will recursively add the values from the array to the textboxes. which implements the following manually written code
a1.text = [arrayName objectAtIndex:1];
a1.text = [arrayName objectAtIndex:2];
...........
a99.text = [arrayName objectAtIndex:99];
can any one help me please...
Thanks in advance,
Shibin
When you create you controls (e.g. textfields) set their tag property to a value that can later be used to determine an index for corresponding value in your array:
for (int index = startTag; index <= endTag; ++index){
UITextField* field = (UITextField*)[view viewWithTag:index]; // or [view viewWithTag:index + someValue];
field.text = [arrayName objectAtIndex:index];
}