-[__NSCFNumber length]: unrecognized selector sent to instance - objective-c

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"myCell"];
if (cell == nil)
{
[tableView registerNib:[UINib nibWithNibName:#"CustomCell" bundle:nil] forCellReuseIdentifier:#"myCell"];
cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"myCell"];
}
if (cell.tag == 0)
{
}
str = [jsonData objectAtIndex:cell.tag];
NSDictionary *dict = [jsonData objectAtIndex:indexPath.row];
cell.lblDeliveryTime.text = [dict valueForKey:#"sysord_ExpectedDeliveryTime"];
cell.lblOrderPlacedTime.text = [dict valueForKey:#"sysord_OrderDateTime"];
cell.lblDeliveryPickUP.text = [dict valueForKey:#"sysord_DeliveryType"];
NSDictionary *dict1 = [jsonData objectAtIndex:cell.tag];
orderidString = [dict1 valueForKey:#"sysord_ID"];
DetailViewController *detailVC=[[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
return cell;
}

One of the values is not a NSString but NSNumber.
I would advise you to first check the type but if you want to always convert to string, you can use:
cell.lblDeliveryPickUP.text = [NSString stringWithFormat:#"%#", dict[#"sysord_DeliveryType"]];

When you assign an object from dictionary to a text, you need to make sure it's a NSString type.
cell.lblDeliveryTime.text = [[dict objectForKey:#"sysord_ExpectedDeliveryTime"] stringValue];
cell.lblOrderPlacedTime.text = [[dict objectForKey:#"sysord_OrderDateTime"] stringValue];
cell.lblDeliveryPickUP.text = [[dict objectForKey:#"sysord_DeliveryType"] stringValue];

Related

Populating 2nd table in Objective C

I have two tables (Cell Identifier: Call and Cell Identifier: Cell2). I'm passing two arrays of Objects (one for each table) however when I go to do this in my tableView my 2nd table is bringing up the same data as my first table and not the data from the 2nd array. My arrays are set globally as NSMutableArray in my .h file. This is where I think the problem is within the code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:#"Current Date"]; //added
// NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:#"Some other key"]; //added
cell.textLabel.text = [array objectAtIndex:indexPath.row];
label1.text = [arrayDate1 objectAtIndex:indexPath.row];
label2.text = [arrayDate2 objectAtIndex:indexPath.row];
// cell.textLabel.text = [NSString stringWithFormat:#"%# %#", currDate, someOtherKey]; //added
return cell;
//This will Load the second table (myTableView2)
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(!cell2)
{
cell2 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
// NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:#"Current Date"]; //added
// NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:#"Some other key"]; //added
cell2.textLabel.text = [array2 objectAtIndex:indexPath.row];
return cell2;
}
It is indeed a problem in this function:
The code as written, will simply run until it gets to the first return cell; line and never run the code after that, therefore always returning an instance of a Cell cell.
In order to use two tables like this, you need to be able to tell them apart. Usually, you store both of them in a declared property. For the rest of this answer, I'll assume that you are doing that and that they are called table1 and table2.
Change you code to look like this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView isEqual:self.table1]) {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:#"Current Date"]; //added
// NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:#"Some other key"]; //added
cell.textLabel.text = [array objectAtIndex:indexPath.row];
label1.text = [arrayDate1 objectAtIndex:indexPath.row];
label2.text = [arrayDate2 objectAtIndex:indexPath.row];
// cell.textLabel.text = [NSString stringWithFormat:#"%# %#", currDate, someOtherKey]; //added
return cell;
} else if ([tableView isEqual:self.table2]) {
//This will Load the second table (myTableView2)
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(!cell2)
{
cell2 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
// NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:#"Current Date"]; //added
// NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:#"Some other key"]; //added
cell2.textLabel.text = [array2 objectAtIndex:indexPath.row];
return cell2;
}
}
// If you reach this point, we don't recognize the table and return `nil`, then the program will crash. Handle this however you want.
return nil;

How to add multiple Objects to Array - Objective C / iOS

I'm new to programming in Objective C
Here is my Dilemma: I'm pulling in a JSON file from the web and I'm able to display one of the elements (currDate) to my tableView but now I want to display more. From the code below I would I get it to display both currDate and prevDate
The logic needs to be changed here:
for (NSDictionary *diction in arrayOfEntry) {
NSString *currDate = [diction objectForKey:#"Current Date"];
a = currDate;
NSString *prevDate = [diction objectForKey:#"Previous Date"];
b = prevDate;
[array addObject:a];
}
[[self myTableView]reloadData];
I'm not sure if I need to change anything here but I'm attaching it to show how I'm displaying the array to my viewTable:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
//cell.textLabel.text = [array objectsAtIndexes:indexPath.row];
return cell;
}
just add another line:
[array addObject:a];
[array addObject:b];
make arrayOfEntry global. in .h file
NSArray *arrayOfEntry;
In numberOfRowsInSection
[arrayOfEntry count]
In tableView: cellForRowAtIndexPath
NSString *currDate = [[arrayOfEntry objectAtIndex:indexPath.row] objectForKey:#"Current Date"]
NSString *prevDate = [[arrayOfEntry objectAtIndex:indexPath.row] objectForKey:#"Previous Date"]
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", currDate, prevDate];
To add multiple elements to an Array in objective c you need to use:
NSMutableArray *newArr = [NSMutableArray new];
Then:
[newArr addObject:dict1];
If you want, you can then set your NSArray you were using to the NSMutuableArray after the addition of objects is complete.

Use of undeclared identifier 'cell'

i'm using a custom tablecell but i have a problem with parsing JSON in my uilabel i'm using the code below, this works great. To put something in my UILabel i simple do
cell.One.text = #"xx";
/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = #"CustomCellIdentifier ";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell"
owner:self options:nil];
for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomCell class]])
cell = (CustomCell *)oneObject;
}
return cell;
}
But when i try to parse something i normally do
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray* lastgame = [json objectForKey:#"items"];
NSDictionary* game = [lastgame objectAtIndex:0];
cell.One.text = [NSString stringWithFormat:#"%#",
[strijd objectForKey:#"One"]
];
}
But on this line i'm recieving an error: (Use of undeclared identifier 'cell') does anyone now how to fix thix?
cell.One.text = [NSString stringWithFormat:#"%#",
[strijd objectForKey:#"One"]
];
cell.One.text = [NSString stringWithFormat:#"%#",
[strijd objectForKey:#"One"]
];
your cell isn't declared in this methode, you have to do this in cellForRowAtIndexPath:
after you declare your cell with: CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];

EXC_BAD_ACCESS is generated in didSelectRowAtIndexPath

Please consider this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Configure the cell.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *dict1 = [rows objectAtIndex:indexPath.row];
NSLog(#"%#", dict1);
if ([dict1 objectForKey:#"faqQues"] != [NSNull null]) {
cell.textLabel.text = [dict1 objectForKey:#"faqQues"];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
faqQuesID = [[rows objectAtIndex: indexPath.row] integerValue];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
//NSString *faqQuesID = [rows objectAtIndex:indexPath.row];
NSLog(#"faqQuesID ######### %#",faqQuesID);
[prefs setInteger:faqQuesID forKey:#"faqQuesID"];
[prefs setInteger:faqTypeID forKey:#"passFaqType"];
helpDetailsViewController *hdVController = [[helpDetailsViewController alloc] initWithNibName:#"helpDetailsViewController" bundle:nil];
[self presentModalViewController:hdVController animated:YES];
[hdVController release];
}
cell.textLabel.textAlignment = UITextAlignmentLeft;
cell.textLabel.font = [UIFont fontWithName:#"Arial" size:13.0];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.highlightedTextColor = [UIColor blueColor];
cell.textLabel.textAlignment = UITextAlignmentCenter;
return cell;
}
// [prefs setInteger:10 forKey:#"faqQuesID"]; if i put manually integer then it does work but when i receive value form indexPath.row then it does show error
// console error is
2011-12-05 18:16:30.312 test[3602:c203] -[__NSCFDictionary integerValue]: unrecognized selector sent to instance 0x719e400
2011-12-05 18:16:30.314 test[3602:c203] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary integerValue]: unrecognized selector sent to instance 0x719e400'
// i have waste a more couple of hours please give me suggestion how i can fix this problem?
In your cellForRowAtIndexPath:
NSDictionary *dict1 = [rows objectAtIndex:indexPath.row];
rows is an array of dictionaries. OK.
In your didSelectRow:
faqQuesID = [[rows objectAtIndex: indexPath.row] integerValue];
Which we can break down into:
NSDictionary *dict = [rows objectAtIndex:indexPath.row];
faQuesID = [dict integerValue];
NSDictionary does not have an integerValue method - which is exactly what the error message is telling you. Presumably you want to get the integer from a particular object in the dictionary.
faqQuesID = [[[rows objectAtIndex: indexPath.row] objectForKey:#"faqQuesID"]integerValue];
Assuming you have an NSNumber stored under the key #"faqQuesID".
So, the your didSelectRow method should be something like:
NSDictionary *faq = [rows objectAtIndex: indexPath.row];
[prefs setInteger:[[faq objectForKey:#"faqQuesID"] integerValue] forKey:#"faqQuesID"];

NSMutableDictionary and NSArray

I am splitting a String:
#"Sam|26|Developer,Hannah|22|Team Leader,Max|1|Dog"
and using NSMutableDictionary to display in a TableViewCell with 3 labels. Code:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *test = #"Sam Parrish|26|Developer,Hannah Rajamets|22|Team Leader,Max Parrish|1|Dog";
testArray = [[NSArray alloc] init];
testArray = [test componentsSeparatedByString:#","];
dict = [NSMutableDictionary dictionary];
for (NSString *s in testArray) {
testArrayNew = [s componentsSeparatedByString:#"|"];
[dict setObject:[testArrayNew objectAtIndex:1] forKey:[testArrayNew objectAtIndex:0]];
[dict setObject:[testArrayNew objectAtIndex:2] forKey:[testArrayNew objectAtIndex:1]];
NSLog(#"Dictionary: %#", [dict description]);
}
[dict retain];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[dict allKeys] count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
untitled *cell = (untitled *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"untitled" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (untitled *) currentObject;
break;
}
}
}
// Configure the cell.
cell.nameLabel.text = [[dict allKeys] objectAtIndex:[indexPath row]];
cell.ageLabel.text = [dict objectForKey:cell.nameLabel.text];
cell.jobLabel.text = [dict objectForKey:cell.ageLabel.text];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
this is displaying 6 TableViewCells when I run, 3 of which are perfect, the other 3 are an assortment of the data. i realise this is to do with the setObject: forKey: but can't seem to find the solution for this to work correctly.
any help much appreciated..
sam
I would store the data as an NSArray holding NSArrays....
- (void)viewDidLoad {
[super viewDidLoad];
NSString *test = #"Sam Parrish|26|Developer,Hannah Rajamets|22|Team Leader,Max Parrish|1|Dog";
data = [[NSMutableArray alloc] init];
for (NSString *s in testArray) {
[data addObject:[s componentsSeparatedByString:#"|"]];
}
}
Then in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
use
// Configure the cell.
NSArray *cellData = [data objectAtIndex:indexPath.row];
cell.nameLabel.text = [cellData objectAtIndex:0];
cell.ageLabel.text = [cellData objectAtIndex:1];
cell.jobLabel.text = [cellData objectAtIndex:2];
If I understand you correctly, you're saying that the first three cells are correct and the other three shouldn't be shown at all? If that's the case, the problem is this:
return [[dict allKeys] count];
Shouldn't it be...
return [testArray count];
...in which case, you need to retain testArray.
In addition, you have a memory leak. You create an instance of NSArray, then you immediately leak it by assigning the return value of componentsSeparatedByString: to the same variable. Have you run the static analyser over your code?