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.
Related
I have a table view and adding items to that like this.`
NSDictionary *memberInfo = [self.currentChannel infoForMemberWithID:memberID];
memberinfo=memberInfo;
cell.textLabel.text = [NSString stringWithFormat:#"%#",[memberinfo objectForKey:#"name"]];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#",[memberinfo objectForKey:#"status"]];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
return cell;
`and it was working fine.Now i want to add a searchbar on to that.i need to load the tableview according to the matched string.But i am loading the tableview like this.I know how to search in an array.I am using
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText.i want to load the table view according to the match of the searchstring ,Can anybody know this?
You can use NSPredicate inside your SearchBar delegate to filter your main array (members or whatever you named it) and catch the results in an another global declared array. Now reload your table with this filtered array.
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[_mArrFilteredList removeAllObjects]; //global array which will contain filtered results
NSString* searchStr = [NSString stringWithFormat:#"*%#*",_srbActivity.text];
NSPredicate* predicate = [NSPredicate predicateWithFormat:#"%K == %#",#"name", searchStr]; //will filter according to "name" key in your dictionary.
[_mArrFilteredList addObjectsFromArray:[_mArrList filteredArrayUsingPredicate:predicate]]; //_mArrList is your main array which has all the dictionaries.
[yourTableView reloadData];
}
Now in your tableview datasource methods, populate table using filtered array(_mArrFilteredList).
For any search implementations we require two lists, one original and other filtered. We will usually use filtered list to fill up the table.
In your case, you can have dictionary as original list. For filtered list you can have a NSArray of values or keys according to requirements.
Filter the array list in search function, reload the table. In cellForRow get the object from the array keys or get key from array values- later object for that key.
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:
I am using an NSArray and alloc it to in viewDidload method.
I have two views in my app and add data to this array from these views. The total number of rows shown in a table according to [array count].
But the problem I'm facing is that when I call the view where I'm using this array from another view then this array realloc and due to this my array size again start from 0. I don't want that. I want the array size start from its last position.
So please help me to remove out this problem where I declare this array or any alternate to do this.
You need to make the array lazily loaded.
Have the view where the array is located have a getter that looks like this:
-(NSArray *)theArray {
if(theArray == nil) {
theArray = [[[NSArray alloc] init] autorelease]; //If using ARC, don't autorelease
}
return theArray;
}
In your firstView's viewDidLoad, just call the array like this:
[self theArray];
Now in your second view, call the first array like this
[firstView theArray];
I currently have a function written called saveWorkout that saves an NSMutableArray to another NSMutableArray from a Singleton class. This function works the first run through, however, when I run it a second time, it erases what was previously stored in element 0 and replaces it with the new array (which is a collection of strings gathered when a user clicks on a table).
Here is my function:
-(IBAction)saveWorkout{
WorkoutManager *workoutManager = [WorkoutManager sharedInstance];
[[workoutManager workouts] insertObject: customWorkout atIndex: 0];
NSLog(#"%#", [workoutManager workouts]);
}
customWorkout is what initialially creates the NSMutableArray (based on what the user clicks). Thus, if my first array is comprised of blah1, blah2, those two values will be stored in the workouts array. However, if I then click blah2, blah 3, the workouts array will have two identicle arrays (blah2, blah3) and it doesn't retain the first array. Any idea why this is happening?
Here is how I form customWorkout:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
[customWorkout insertObject:str atIndex:0];
//Test Code: Prints Array
NSLog(#"%#", customWorkout);
}
I will tell you the logical mistake that you are making....
you are using the same customWorkout object over and over again to insert in the workouts array... (so its the same pointer) whereas what you need to do is to create a copy of the customWorkout array and then insert it in the workout array ... try this instead....
[[workoutManager workouts] insertObject: [[customWorkout mutableCopy] autorelease]atIndex: 0];
this should work unless you are doing something else in your code.
[[workoutManager workouts] insertObject: customWorkout atIndex: 0]; does not copy the contents of customWorkout ... instead it just retains a reference to customWorkout. So your code is simply storing multiple references to the same object, which you end up (unintentionally) editing on the second run through.
You need to either:
Copy the customWorkout object via copy when you store it in workouts
OR:
Assign customWorkout to a new NSMutableArray instance each time after you do a saveWorkout
Either route should keep you from modifying the NSMutableArray you store into the workouts collection. The first option is probably more clear in terms of memory-management...
I need to Write 4 sections in a table where two of them have only a single row.
So is there any other way i could do it instead of using NSArray for just a single row?
You can create two dimensional array using NSMutablearray
You can access them by using
[[array objectAtIndex: ] objectAtIndex: ]
Hope it helps.....
Dont know from where you are getting the data for your 2 single rows but you could make a switch(indexPath.section)/case or an if(indexpath.section == yoursectionnumber) logic in your cellForRowAtIndexPart delegate method and there you can assign a value to your cell.
As DShah wrote, you can use variables and such for this so you could for example do a cell.textLabel.text = myString;
Where myString is an NSString created by you.