For some reasons, I have to extract a cell's text or value from a UITableView. I have the following table method:
- (void)loadCollection_Information
{
_collection_InformationCellData = [[NSMutableArray alloc] init];
NSMutableArray *cells_1 = [[NSMutableArray alloc] init];
NSDictionary *cellContainer_1_1 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"Brett Whiteley", #"Author", #"", #"", #"", #"", nil] forKeys:[NSArray arrayWithObjects:#"Text", #"Detail Text", #"Image", #"Text Color", #"Detail Text Color", #"Accessory", nil]];
[cells_1 addObject:cellContainer_1_1];
NSDictionary *cellContainer_1_2 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"1976", #"Year", #"", #"", #"", #"", nil]
forKeys:[NSArray arrayWithObjects:#"Text", #"Detail Text", #"Image", #"Text Color", #"Detail Text Color", #"Accessory", nil]];
[cells_1 addObject:cellContainer_1_2];
NSDictionary *cellContainer_1_3 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"Canvas, Oil", #"Materials", #"", #"", #"", #"", nil] forKeys:[NSArray arrayWithObjects:#"Text", #"Detail Text", #"Image", #"Text Color", #"Detail Text Color", #"Accessory", nil]];
[cells_1 addObject:cellContainer_1_3];
NSDictionary *cellContainer_1_4 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"Social Comment & Hard Edged Abstraction", #"Style", #"", #"", #"", #"", nil]
forKeys:[NSArray arrayWithObjects:#"Text", #"Detail Text", #"Image", #"Text Color", #"Detail Text Color", #"Accessory", nil]];
[cells_1 addObject:cellContainer_1_4];
NSDictionary *cellContainer_1_5 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"182.0 high * 200.0 wide cm", #"Size", #"", #"", #"", #"", nil]
forKeys:[NSArray arrayWithObjects:#"Text", #"Detail Text", #"Image", #"Text Color", #"Detail Text Color", #"Accessory", nil]];
[cells_1 addObject:cellContainer_1_5];
NSDictionary *sectionContainer_1 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"Collection Information", cells_1, #"", nil]
forKeys:[NSArray arrayWithObjects:#"Title", #"Cells", #"Footer Title", nil]];
[_collection_InformationCellData addObject:sectionContainer_1];
_collection_InformationSelectedRow = 0;
_collection_InformationSelectedSection = 0;
_collection_InformationShowHeader = YES;
[_collection_Information setEditing:NO];
[_collection_Information reloadData];
}
and didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (tableView == _collection_Information) {
if (indexPath.section == 0 && indexPath.row == 0) {
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"Brett_WhiteleyViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
if (indexPath.section == 0 && indexPath.row == 1) {
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"Year_1960s_70sViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
if (indexPath.section == 0 && indexPath.row == 2) {
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"Canvas_and_OilViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
if (indexPath.section == 0 && indexPath.row == 3) {
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"SC_and_HE_AbstractionViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
}
//Get the Dictionary Object at index 0, to check out the object.
NSLog(#"%#",[_collection_InformationCellData objectAtIndex:0]);
//Store the Objects from dictionary in temp Array.
NSArray *temp = [[_collection_InformationCellData objectAtIndex:indexPath.section] valueForKey:#"Cells"];
//Get the object based on Row Selection in UITableView.
NSLog(#"%#",[[temp objectAtIndex: indexPath.row] valueForKey:#"Text"]);
}
I need to extract the NSStrings such as: Brett Whiteley, 1976, Canvas, Oil, Social Comment & Hard Edged Abstraction separately, and put it into follow code in another method:
-(NSString *)maxmsg
{
NSString *msg;
int maxint=-1;
RecommendationData *obj = [RecommendationData getInstance];
if(obj.author<=5 && obj.year<=5 && obj.material<=5 && obj.style<=5)
{
msg=#"nothing";
}
else if( obj.author >= obj.year && obj.author >= obj.material && obj.author >= obj.style)
{
maxint=obj.author;
//the code for placing the value
obj.maxValue= (place value here);
msg=#"It seems that you are interested in the author of this collection.";
}
else if(obj.year >= obj.author && obj.year >= obj.material && obj.year >= obj.style)
{
maxint=obj.year;
//the code for placing the value
obj.maxValue= (place value here);
msg=#"It seems that you are interested in this period of the collection.";
}
else
{
msg=#"equal";
}
return msg;
}
Can anyone tell me how to do it?
To receive "Brett Whiteley" from another method you can call
_collection_InformationCellData[ CellContainerIndex ][ #"Cells" ][ 0 ][ "Text" ]
but you need to save somewhere/to know the correct CellContainerIndex. May be it is just 0 in your case?
Here is the way to perform it :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the Dictionary Object at index 0, to check out the object.
NSLog(#"%#",[_collection_InformationCellData objectAtIndex:0]);
//Store the Objects from dictionary in temp Array.
NSArray *temp = [[_collection_InformationCellData objectAtIndex:indexPath.section] valueForKey:#"Cells"];
//Get the object based on Row Selection in UITableView.
NSLog(#"%#",[[temp objectAtIndex: indexPath.row] valueForKey:#"Text"]);
}
You need to access the Array using Index and getting the values using key from that object.
Related
I have got a small problem with the below service data
{
"DataTable": {
"DataList1": {
"StudentName": "Rakesh",
"StudentId": "13",
"StudentAge": "19",
"StudentAddress": "NewYork",
},
"DataList2": [{
"TeacherName": "Abhinav",
"TeacherId": "309",
"TeacherAge": "34",
"TeacherAddress": "NewYork",
}]
}
}
i Can get the data from DataList1 and cannot know how to get the data from DataList2. Below is the code what i have tried. Please help to find out the solution. Thanks in Advance
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[jsonArray removeAllObjects];
NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
NSMutableDictionary *sdf = [(NSDictionary*)[responseString JSONValue] objectForKey:#"DataTable"];
NSMutableArray * myArray = [[NSMutableArray alloc] init];
if (([(NSString*)sdf isEqual: [NSNull null]])) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Information" message:#"Currently there are no Data" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert setTag:1];
[alert show];
}else {
[myArray addObject:[sdf objectForKey:#"DataList1"]];
jsonArray=[myArray mutableCopy];
refilldict=[jsonArray objectAtIndex:0];
NSArray *keys = [refilldict allKeys];
for(int p=0 ; p<[keys count]; p++ )
{
NSString *value=[refilldict objectForKey:[keys objectAtIndex:p]];
if ([value isEqual: [NSNull null]]||[value length]==0) {
[refilldict setValue:#"" forKey:[keys objectAtIndex:p]];
}
}
lblStudentName.text = [refilldict objectForKey:#"StudentName"];
lblStudentId.text = [refilldict objectForKey:#"StudentId"];
lblStudentAge.text = [refilldict objectForKey:#"StudentAge"];
lblStudentAddress.text = [refilldict objectForKey:#"StudentAddress"];
}
self.navigationController.navigationBar.userInteractionEnabled = YES;
[HUD hide:YES];
[HUD removeFromSuperview];
HUD=nil;
}
Please use the bellow code and pass your Initial Json dictionary in it.
-(void)parseJsonData:(NSDictionary *)jsonDictionary{
for(int i=0;i<[jsonDictionary allKeys].count;i++){
NSString *keyName = [[jsonDictionary allKeys] objectAtIndex:i];
id objValue = [jsonDictionary objectForKey:keyName];
if([objValue isKindOfClass:[NSArray class]]){
NSArray *dataList2Array = (NSArray *)objValue;
NSLog(#"DataList2 Is :--%#",dataList2Array);
}
else {
NSDictionary *dataList1 = (NSDictionary *)objValue;
NSLog(#"DataList1 Is :--%#",dataList1);
}
}
}
you can get easily like
[myArray addObject:[sdf objectForKey:#"DataList1"]];
its started with array of dictionary , so you need to store your second object to array and take from index.
NSArray *temp = [sdf objectForKey:#"DataList2"];
if(temp.count >0)
{
lblTeacherName.text = temp[0][#"TeacherName"];
lblTeacherId.text = temp[0][#"TeacherId"];
lblTeacherAge.text = temp[0][#"TeacherAge"];
lblTeacherAddress.text = temp[0][#"TeacherAddress"];
}
i have an array with 12 sections and i need to replace value at index.
My test code:
NSMutableArray *hm = [[NSMutableArray alloc] initWithObjects:#{#"first": #[#"test1", #"test2"]}, #{#"second": #[#"test1"]}, nil];
NSLog(#"%#", [hm valueForKey:#"first"][0][0] );
[[hm valueForKey:#"first"][0] replaceObjectAtIndex:0 withObject:#"lol"];
NSLog(#"%#", hm);
First NSLog returns : test1 - its ok
When replace - crash with -[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x7fde53d2f700
I need to change test1 to something.
Wha am i doing wrong please?
NSMutableArray *hm = [[NSMutableArray alloc] initWithObjects:#{#"first": #[#"test1", #"test2"]}, #{#"second": #[#"test1"]}, nil];
NSLog(#"%#", [hm valueForKey:#"first"][0][0] );
//Your inner array is immutable, change it to mutable and replace the object, That's it.
NSMutableArray *array = [[hm valueForKey:#"first"][0] mutableCopy];
[array replaceObjectAtIndex:0 withObject:#"lol"];
[hm replaceObjectAtIndex:0 withObject:array];
NSLog(#"%#", hm);
NSMutableArray *arr = [[NSMutableArray alloc] initwithArray[[hm objectAtIndex:0]objectForKey:#"first"]];
[arr replaceObjectAtIndex:0 withObject:#"lol"];
[hm replaceObjectAtIndex:0 withObject:arr];
You have to make mutable dictionary and array structure
NSMutableArray* names = [NSMutableArray arrayWithObjects:
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Joe",#"firstname",
#"Bloggs",#"surname",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Simon",#"firstname",
#"Templar",#"surname",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Amelia",#"firstname",
#"Pond",#"surname",
nil],
nil];
NSLog(#"Before - %#",names);
[names replaceObjectAtIndex:0 withObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Joe_New",#"firstname",
#"Bloggs_New",#"surname",
nil]];
NSLog(#"After - %#",names);
Before - (
{
firstname = Joe;
surname = Bloggs;
},
{
firstname = Simon;
surname = Templar;
},
{
firstname = Amelia;
surname = Pond;
}
)
After - (
{
firstname = "Joe_New";
surname = "Bloggs_New";
},
{
firstname = Simon;
surname = Templar;
},
{
firstname = Amelia;
surname = Pond;
}
)
NSMutableArray *hm = [[NSMutableArray alloc] initWithObjects:#{#"first": #[#"test1", #"test2"]}, #{#"second": #[#"test1"]}, nil];
NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithDictionary:[hm objectAtIndex:0]];
NSMutableArray *array=[NSMutableArray arrayWithArray:[dic objectForKey:#"first"]];
[array replaceObjectAtIndex:0 withObject:#"lol"];
[dic setObject:array forKey:#"first"];
[hm replaceObjectAtIndex:0 withObject:array];
O/P (ViewController.m:48) (
(
lol,
test2
),
{
second = (
test1
);
}
)
I'm having a tableview. And in my didselectrow I get the right object that I want to pass to the next controller.
This is my code:
id<NavigateDelegate> strongDelegate = self.delegate;
NSDictionary * dictionary = [_dataArray objectAtIndex:indexPath.section];
NSArray * array = [dictionary objectForKey:#"data"];
POI * poi = [array objectAtIndex:indexPath.row];
NSLog(#"%#", poi);
NSLog(#"Category %#", poi.rank.name);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
DetailsViewController * detailsController = [[DetailsViewController alloc] init];
detailsController.details = [[NSDictionary alloc] initWithObjectsAndKeys:poi.name, #"name", poi.address, #"address", poi.details, #"details", poi.title, #"title", poi.url, #"url", nil];
if ([strongDelegate respondsToSelector:#selector(navigateFromTo:to:)]) {
[strongDelegate navigateFromTo:self to:detailsController];
}
Then in my delegate receiver I push the controller like:
- (void) navigateFromTo:(POITableViewController *)viewController to:(UIViewController *)toController
{
[self.navigationController pushViewController:toController animated:YES];
}
But still my DetailsController his details NSdictionary is null.
What am I doing wrong?
I found the problem. The viewdid load is called before my setter is called.
So I changed the init method like this:
-(instancetype) initWithPoi:(NSDictionary *) poi
{
self = [super init];
if(self)
{
NSLog(#"Init DetailsViewController");
_details = poi;
[self initializeViews];
}
return self;
}
that way the controller has the poi details from the start.
What I've done so far to make a search bar work...
- (void)viewDidLoad
{
[self makeData];
[super viewDidLoad];
self.mySearchBar.delegate = self;
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
//Feedback button code here
UIBarButtonItem *feedback;
feedback = [[UIBarButtonItem alloc] initWithTitle:#"Contact" style:UIBarButtonItemStylePlain
target:self action:#selector(showEmail:)];
self.navigationItem.leftBarButtonItem = feedback;
[self.tableView reloadData];
self.tableView.scrollEnabled = YES;
}
-(void) makeData; {
list = [[NSMutableArray alloc] init];
[list addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"Acceleration", #"name" ,
#"acceleration.png", #"image" ,
#"Acceleration", #"description" ,nil]];
[list addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"Alternating Current", #"name" ,
#"alternating current.png", #"image" ,
#"Alternating Current", #"description" ,nil]];
[list addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"Ampere", #"name" ,
#"ampere.png", #"image" ,
#"Ampere", #"description" ,nil]];
[list addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"Amplitude", #"name" ,
#"amplitude.png", #"image" ,
#"Amplitude", #"description" ,nil]];
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length == 0) {
isFiltered = FALSE;
}
else
{
isFiltered = TRUE;
filteredList = [[NSMutableArray alloc] init];
for (NSString *str in list) {
NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (stringRange.location != NSNotFound) {
[filteredList addObject:str];
}
}
}
[self.myTableView reloadData];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.myTableView reloadData];
}
Yet each time I try to type something in the app crashes and I get this message:
2013-05-29 11:36:09.734 PhysEX[4770:c07] -[__NSDictionaryM rangeOfString:options:]: unrecognized selector sent to instance 0x9468cd0
2013-05-29 11:36:09.735 PhysEX[4770:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM rangeOfString:options:]: unrecognized selector sent to instance 0x9468cd0'
How do I get my search to work?
The following piece of code in textDidChange delegate is not correct:
for (NSString *str in list) {
...
}
It is because objects in the list array are not NSString but NSDictionary. Hence the error message saying that NSDictionary does not recognize the selector rangeOfString:options: because that selector is for NSString instance.
You need to search through NSDictionary values instead.
for (NSDictionary *theDictionary in list) {
for (NSString *key in theDictionary) {
NSString *value = [theDictionary objectForKey:key];
NSRange stringRange = [value rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (stringRange.location != NSNotFound) {
[filteredList addObject:theDictionary];
break;
}
}
}
I have a mainViewController which pushes a detailViewController onto the screen.
The items (images, navigation bar title, textview, and a button) in the detailViewController are all loaded from an NSMutableDictionary in the MainViewController depending on which image in it is tapped.
I also have a button in the detailViewController that when selected loads a mapview which shows the user's location. When the user taps a button a pin is dropped marking this location with a annotation callout title.
I want the title of the mapview and the annotation title to be the same as the detailViewController's which is loaded from the MainViewController's NSMutableDictionary.
Right now I am trying to use the detailViewController's title as the determining factor in the mapview's title and annotation title. However this isn't working.
Here is my code in MainViewController .m.
- (void) coverflowView:(TKCoverflowView*)coverflowView coverAtIndexWasDoubleTapped:(int)index{
SomeDetailViewController *detailViewController = [[SomeDetailViewController alloc] initWithNibName:#"SomeDetailViewController" bundle:nil];
if ((int)index == 0) {
NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"0002.jpg", #"imagekey",
#"This is tree 0002", #"textkey",
#"0002.image.png",#"imagekey2",
#"0002 title", #"headerkey",
#"www.0003.com", #"address",nil];
NSLog(#"%#", [myDictionary objectForKey:#"imagekey"]);
NSLog(#"%#", [myDictionary objectForKey:#"textkey"]);
NSLog(#"%#", [myDictionary objectForKey:#"imagekey2"]);
NSLog(#"%#", [myDictionary objectForKey:#"address"]);
NSLog(#"%#", [myDictionary objectForKey:#"headerkey"]);
detailViewController.myDictionary = myDictionary;
}
else if ((int)index == 1) {
NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"0003.jpg", #"imagekey",
#"This is 0003", #"textkey",
#"0003.image.png",#"imagekey2",
#"0003 title", #"headerkey",
#"www.0003.com", #"address",nil];
NSLog(#"%#", [myDictionary objectForKey:#"imagekey"]);
NSLog(#"%#", [myDictionary objectForKey:#"textkey"]);
NSLog(#"%#", [myDictionary objectForKey:#"imagekey2"]);
NSLog(#"%#", [myDictionary objectForKey:#"address"]);
NSLog(#"%#", [myDictionary objectForKey:#"headerkey"]);
detailViewController.myDictionary = myDictionary;
}
[self.navigationController pushViewController:detailViewController animated:YES];
}
Here is my code in the detailViewController.m.
This loads my Dictionary strings (The bold part is loading the navigation bar title).
- (void)viewDidLoad {
NSString *filePath =[self pathOfFile];
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]){
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
myText.text = [array objectAtIndex:0];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
[super viewDidLoad];
imageView .image = [[UIImage alloc] init];
self.imageView.image =[UIImage imageNamed:[(NSMutableDictionary *)myDictionary objectForKey:#"imagekey"]];
imageView2 .image = [[UIImage alloc] init];
self.imageView2.image =[UIImage imageNamed:[(NSMutableDictionary *)myDictionary objectForKey:#"imagekey2"]];
self.myText.text =[(NSMutableDictionary *)myDictionary objectForKey:#"textkey"];
**self.navigationItem.title = [(NSMutableDictionary *)myDictionary objectForKey:#"headerkey"];**
NSLog(#"%#", [myDictionary objectForKey:#"imagekey"]);
NSLog(#"%#", [myDictionary objectForKey:#"imagekey2"]);
NSLog(#"%#", [myDictionary objectForKey:#"textkey"]);
NSLog(#"%#", [myDictionary objectForKey:#"headerkey"]);
}
Here is where I am trying to load the navigation bar title and annotation title using the current navigation bar title as the determining factor.
This is the only thing I can think of to use since there is no stable element. Everything in the detailViewController is loaded from the MainViewController NSDictionary.
-(IBAction)pushMap:(id) sender{
SomePositionViewController *somePosition = [[SomePositionViewController alloc] init];
if ((self.navigationItem.title = #"0002 title")) {
NSMutableDictionary *mapDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"0002a title", #"headerkey2",
#"0002 annotation title", #"titlekey",nil];
treePosition.mapDictionary = mapDictionary;
NSLog(#"%#", [mapDictionary objectForKey:#"titlekey"]);
NSLog(#"%#", [mapDictionary objectForKey:#"headerkey2"]);
}
else if((self.navigationItem.title = #"0003 title")) {
NSMutableDictionary *mapDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"0003a title", #"headerkey2",
#"0003 annotation title", #"titlekey",nil];
treePosition.mapDictionary = mapDictionary;
NSLog(#"%#", [mapDictionary objectForKey:#"titlekey"]);
NSLog(#"%#", [mapDictionary objectForKey:#"headerkey2"]);
}
[self.navigationController pushViewController:somePosition animated:YES];
}
Does anyone have any idea how I can accomplish this? I have been wracking my brain but to no avail. HELP!
One thing I spotted is that you are comparing strings the wrong way. You use
if ((self.navigationItem.title = #"0002 title"))
but should use
if (([self.navigationItem.title isEqualToString:#"0002 title"]))
Comparing strings in your way will always fail.