I am getting the above mentioned error and do not know why because I implemented the same code in another example. "services" is my NSMutableArray and "service name" is the label on table view with Tag 2. In viewdidload I fill the service array with objects.
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"servicesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UILabel *servicesName;
UILabel *nrofService;
if(tableView == self.servicetable)
{
servicesName = (UILabel*) [cell viewWithTag:2];
servicesName.text = [[services objectAtIndex:0] objectAtIndex:indexPath.row];
}
return cell;
}
The problem is on the below line:
servicesName.text = [[services objectAtIndex:0] objectAtIndex:indexPath.row];
Check [services objectAtIndex:0] is returning NSString instead of NSArray or NSMutableArray. So you are calling objectAtIndex:indexPath.row on NSString which causes the exception:
NSCFConstantString objectAtIndex unrecognized selector sent to
instance
So set your UILabel text as below:
servicesName.text = [services objectAtIndex:indexPath.row];
Related
I am trying to set up a UITableView inside of a UIViewController. I am using storyboard. but when running it on the simulator, the tableview does not display any content. Here is what i tried in Xcode:
1) Drag a table view on UIView.
2) Connect it's outlets (dataSource and delegate).
3) I get response from server like:
[{"email_id":"adas#faga.gs","id":66,"mobile_no":"1236547895","name":"asad","relation":"dsfs"},{"email_id":"raj#ghj.com","id":67,"mobile_no":"5632145412","name":"raj","relation":"xyz"}]
4)Now what i want to do:
In 1st cell i want to display,
name:asad mobile:1236547895 email:adas#faga.gs relation:dsfs
and in 2nd cell,
name:raj mobile:5632145412 email:raj#ghj.com relation:xyz
But i have no idea about how to do this.Please anyone can solve my issue. help will be appreciable.
I do like following way but it's not working.
a) Add a new file to the project, with the Objective-C class template. Name it EmergencyContactCell and make it a subclass of UITableViewCell.
b) then in EmergencyContactCell.h
#interface EmergencyContactCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *name;
#property (nonatomic, weak) IBOutlet UILabel *email;
#property (nonatomic, weak) IBOutlet UILabel *mobile;
#property (nonatomic, weak) IBOutlet UILabel *relation;
c) then MainStoryboard.storyboard, select the prototypecell and in the Identity Inspector change its class to EmergencyContactCell
d) Then connect all outlets
e) I am using AFNetworking to take response from server, when i got response, after that i do like following way:
NSArray *ResponseArray = [NSJSONSerialization JSONObjectWithData: responseObject options: kNilOptions error: nil];
if (ResponseArray.count >0)
{
menuItems = [ResponseArray mutableCopy];
[yourTableviewname reloadData];
}
f) and for display it on cell,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"EmergencyContactCell";
//static NSString *simpleTableIdentifier = #"cell";
EmergencyContactCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
// NSDictionary *content = [menuItems objectAtIndex:indexPath.row];
// cell.textLabel.text = [NSString stringWithFormat:#"Name:%#, Mobile:%#, Email:%#, Relation:%#",content[#"name"],content[#"mobile_no"],content[#"email_id"],content[#"relation"]];
//
return cell;
}
I'm not getting what to do at last??
Step-1
No need of this
NSArray *ResponseArray = [NSJSONSerialization JSONObjectWithData: responseObject options: kNilOptions error: nil];
NSMutableArray *finalArray = [NSMutableArray array];
for (NSDictionary *temp in ResponseArray) {
[finalArray addObject:temp[#"name"]];
}
NSLog(#"%#",finalArray);
if(finalArray != nil && finalArray.count > 0){
menuItems=[NSArray arrayWithArray:finalArray];
NSLog(#"menu items: %#",menuItems);
}
else{
NSLog(#"zero values from server");
}
Simply Do like
NSArray *ResponseArray = [NSJSONSerialization JSONObjectWithData: responseObject options: kNilOptions error: nil];
if (ResponseArray.count >0)
{
menuItems = [ResponseArray mutableCopy];
[yourTableviewname reloadData];
}
Step-2
for your answer continution
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSDictionary *content = [menuItems objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"Name:%#, Mobile:%#, Email:%#, Relation:%#",content[#"name"],content[#"mobile_no"],content[#"email_id"],content[#"relation"]];
return cell;
}
Updated Answer
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"EmergencyContactCell";
EmergencyContactCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *content = [menuItems objectAtIndex:indexPath.row];
cell.name.text = [NSString StringWithFormat:#"Name:%#",content[#"name"]];
cell.email.text = [NSString StringWithFormat:#"email:%#",content[#"email"]];
cell.mobile.text = [NSString StringWithFormat:#"mobile:%#",content[#"mobile"]];
cell.relation.text = [NSString StringWithFormat:#"relation:%#",content[#"relation"]];
return cell;
}
You need to do 2 things
After getting response set that into one NSArray. Declare NSArray
global one. And your code should be like
NSArray *arrayPersonalInfo = responseObject; // responseObject is response
got from server. Then call [tableView reloadData];
Now update your table view methods to
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return arrayPersonalInfo.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSDictionary *dictionaryMenu = [arrayPersonalInfo objectAtIndex:indexPath.row]; // It will save dictionary object of index
cell.textLabel.text = [NSString stringWithFormat:#"Name:%# Mobile:%# Email:%# Relation:%#",[dictionaryMenu valueForKey:#"name"],[dictionaryMenu valueForKey:#"mobile_no"],[dictionaryMenu valueForKey:#"email_id"],[dictionaryMenu valueForKey:#"relation"]];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
return cell;
}
If needed add other table view methods like
heightForRowAtIndexPath and numberOfSections
If you prefer using storyboard, you can actually add a label into a prototype cell, make sure you set the prototype's cellIdentifier correctly to use it in codes. Then set a unique tag to your label. From your codes you can call viewWithTag from your cell's contentView to get the label and edit it's text property.
I'm trying to implement a right swipe to the left but is giving this error :
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '- [UITableViewCell
setDelegate]: unrecognized selector sent to instance 0x7fbadb85d710'
this error of the lines:
cell.delegate = self;
cell.allowsMultipleSwipe = allowMultipleSwipe;
cell.rightSwipeSettings.transition = data.transition;
cell.rightExpansion.buttonIndex = data.rightExpandableIndex;
cell.rightExpansion.fillOnTrigger = YES;
cell.rightButtons = [self createRightButtons:data.rightButtonsCount];
my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MGSwipeTableCell *cell ;
static NSString * reuseIdentifier = #"listAlbuns";
cell = [self.tblAlbum dequeueReusableCellWithIdentifier:reuseIdentifier];
if (!cell) {
cell = [[MGSwipeTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
NSString *sectionTitle = [albumSectionTitles objectAtIndex:indexPath.section];
NSMutableDictionary *sectionAlbuns = [albuns objectForKey:sectionTitle];
NSString *title = [sectionAlbuns objectForKey: [NSString stringWithFormat:#"%i", indexPath.row]];
NSMutableDictionary *album = [baseController getCurrentItemByTitle: albunsBD :title];
TestData *data = [tests objectAtIndex:indexPath.row];
NSInteger ID = [album objectForKey: #"1"];
cell.textLabel.text = [album objectForKey: #"2"];
cell.detailTextLabel.text = [[albumDAO selectArtistByAlbumID: ID] uppercaseString];
cell.imageView.image = [UIImage imageNamed: #"icones-categorias-album"];
cell.imageView.image = [albumDAO getThumb: ID];
cell.tag = [album objectForKey: #"1"];
cell.delegate = self;
cell.allowsMultipleSwipe = allowMultipleSwipe;
cell.rightSwipeSettings.transition = data.transition;
cell.rightExpansion.buttonIndex = data.rightExpandableIndex;
cell.rightExpansion.fillOnTrigger = YES;
cell.rightButtons = [self createRightButtons:data.rightButtonsCount];
return cell;
}
anyone have any idea what can be?
Since you are trying to use a UITableViewCell subclass, MGSwipeTableViewCell, you must ensure the cell you are getting back from dequeueReusableCellWithIdentifier returns an MGSwipeTableViewCell.
Firstly, you can remove the if (!cell) { ... } block, as dequeueReusableCellWithIdentifier should return a UITableViewCell if you have the identifier set in a .xib or on the storyboard. Having the extra block can make this code harder to debug if the cell doesn't have a reuse identifier on the storyboard or .xib.
Next, in your .xib or storyboard where the cell you are trying to dequeue exists, select the prototype UITableViewCell and look at the box named "Class" under the section "Custom Class". Change this to MGSwipeTableViewCell, as I suspect it is set to UITableViewCell. This should fix the problem. Here is the area in Xcode I am talking about:
You should be able to set cell.delegate and other MGSwipeTableViewCell properties after you dequeue a real MGSwipeTableViewCell.
I'm trying to convert my NSDictionary (storageData) to an NSArray, as I need to pass the data (title) contained in the dictionary through a segue. I've implemented the below (see last method, the tableview is simply to provide some context), but a crash occurs with the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]:
unrecognized selector sent to instance
Any idea how to fix?
Viewcontrolller.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoctorsTableIdentifier = #"StorageItemTableViewCell";
StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"StorageItemTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSDictionary *tmp = [self.storageData objectForKey:[NSString stringWithFormat:#"%d",(long)indexPath.row]];
[[cell itemName] setText:(NSString *)[tmp objectForKey:#"title"]];
NSString *title = [tmp objectForKey:#"title"];
[[cell itemName] setText:title];
NSDictionary *node = [self.descripData objectAtIndex:indexPath.row];
[[cell itemDescrip] setText:[node objectForKey:#"body"]];
NSString *secondLink = [[self.descripData objectAtIndex:indexPath.row] objectForKey:#"photo"];
[cell.itemPhoto sd_setImageWithURL:[NSURL URLWithString:secondLink]];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *values = [self.storageData allKeys];
ItemDetailViewController *detailViewController = [[ItemDetailViewController alloc]
initWithNibName:#"ItemDetailViewController" bundle:nil];
detailViewController.title = [[values objectAtIndex:indexPath.row] objectForKey:#"title"];
detailViewController.itemDetail = [self.descripData objectAtIndex:indexPath.row];
detailViewController.secondLink = self.descripData[indexPath.row][#"photo"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
The error states that you are calling objectForKey in an object of type NSString, which doesn't have that method declared.
So put a breakpoint in the beginning of the method, step over each line one by one until crashes and then check what is the variable that you are thinking is a Dictionary when it is a String.
I believe if you would extract allValues instead of allKeys then you could only:
[values objectAtIndex:indexPath.row]
Since you are using a index as indexPath.row. Let me know if this helps.
I've got the code below to fill up an array of notifications. This array is needed to fill up a menu in my iOS app with notifications.
// extract specific value...
NSArray *switchValues = [res objectForKey:#"data"];
NSLog(#"%#", switchValues);
for (NSDictionary *switchValue in switchValues) {
NSString *text = [switchValue objectForKey:#"text"];
NSLog(#"%#", text);
[self.notificationsArray addObject:text];
}
Further down the line I then do this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSMutableArray *titles = self.notificationsArray;
cell.textLabel.text = titles[indexPath.row];
return cell;
}
However, I keep getting an empty array. What am I doing wrong?
Why don't you do
[notificationsArray objectAtIndex:indexPath.row]
instead of
NSMutableArray *titles = self.notificationsArray;
cell.textLabel.text = titles[indexPath.row];
?
If you really dont want, at least do
NSMutableArray *titles = [NSMutableArray arrayWithArray:self.notificationsArray];
And in the first piece of code, what are the Console Logs? Are you sure you enter in the for loop?
Hey guys I am having an issue passing a JSON return type through an NSArray
.h file:
#property (strong, nonatomic) NSArray *googlePlacesArrayFromAFNetworking;
#property (strong, nonatomic) NSArray *finishedGooglePlacesArray;
.m file:
Here is the returned section:
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:returnData //1
options:kNilOptions
error:&error];
self.googlePlacesArrayFromAFNetworking = [json objectForKey:#"original_request"];
[self.tableView reloadData];
Now from there it gets sent to here:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *tempDictionary= [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];
cell.textLabel.text = [tempDictionary objectForKey:#"name"];
if([tempDictionary objectForKey:#"location"] != NULL)
{
cell.detailTextLabel.text = [NSString stringWithFormat:#"Rating: %# of 5",[tempDictionary objectForKey:#"name"]];
}
else
{
cell.detailTextLabel.text = [NSString stringWithFormat:#"Not Rated"];
}
return cell;
}
But it breaks on
NSDictionary *tempDictionary= [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];
with an error message:
2014-03-08 13:53:26.772 Ripple[45593:70b] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8b72810
2014-03-08 13:53:26.776 Ripple[45593:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8b72810'
I am not sure what is going on, suggestions or thoughts?
edit:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.googlePlacesArrayFromAFNetworking count];
}
JSON
responseString: {"status":"ok","code":0,"message":"Testing this awesome application out!","from":"Admin Team","original_request":{"name":"david","location":"awesome","trait":"funny"}}
Update:
Screen shot of error:
According to JSON log, [json objectForKey:#"original_request"] returns anNSDictionary`. So First you need to change this line :
#property (strong, nonatomic) NSArray *googlePlacesArrayFromAFNetworking;
to
#property (strong, nonatomic) NSDictionary *googlePlacesArrayFromAFNetworking;
It's normal that you get an error after that because NSDictionary doesn't have objectAtIndex selector. You can enumerate through keys and values in NSDictionary.
Than, change your cellForRowAtIndexPath method to :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.googlePlacesArrayFromAFNetworking objectForKey:#"name"];
if([self.googlePlacesArrayFromAFNetworking objectForKey:#"location"] != NULL)
{
cell.detailTextLabel.text = [NSString stringWithFormat:#"Rating: %# of 5",[self.googlePlacesArrayFromAFNetworking objectForKey:#"name"]];
}
else
{
cell.detailTextLabel.text = [NSString stringWithFormat:#"Not Rated"];
}
return cell;
}
It's look like you don't understand what you get from your json...
so, try this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//get the correct type
NSDictionary *tempDictionary = (NSDictionary *)self.googlePlacesArrayFromAFNetworking;
NSString *key = [[tempDictionary allKeys]objectAtIndex:indexPath.row];
cell.textLabel.text = key;
cell.detailTextLabel.text = [tempDictionary objectForKey:key];
return cell;
}