I’ve got this error when populating a tableview from an nsmutablearray.
The arrays are added to the dictionary and the dictionary added to the mutable array, but the app crashes each time the view is loaded with error:
[__NSArrayI isEqualToString:]: unrecognized selector sent to instance
0x8a87e60 2014-05-24 12:23:02.589 jwStudy[77652:907] * Terminating
app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSArrayI isEqualToString:]: unrecognized selector sent to
instance 0x8a87e60'
The log output confirms that the arrays and keys are added correctly.
But why does the cell in the cellForRowAtIndexPath: method throw an error?
Here’s the code:
- (void)viewDidLoad
{
[super viewDidLoad];
languageArray = [[NSMutableArray alloc] init];
languages = [[NSArray alloc] initWithObjects:#"Abbey", #"Abkhasisk", #"Abua", nil];
downloadURL = [[NSArray alloc] initWithObjects:#"http://download.jw.org/files/content_assets/bb/502013341_ABB_cnt_1_r480P.mp4",
#"http://download.jw.org/files/content_assets/3c/502013341_ABK_cnt_1_r480P.mp4",
#"http://download.jw.org/files/content_assets/7e/502013341_AU_cnt_1_r720P.mp4", nil];
mutedict = [NSDictionary dictionaryWithObjectsAndKeys:languages, #"FirstKey", downloadURL, #"SecondKey", nil];
[languageArray addObject:mutedict];
NSLog(#"%#",languageArray);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return languageArray.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];
}
// ERROR HERE!!!
cell.textLabel.text = [[languageArray objectAtIndex:indexPath.row] objectForKey:#"FirstKey”];
if ([checkCellLanguage isEqual:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
#Farhan and Joy.
Thank you both for prompt reply. I have realized now that I wasn't clear enough in my question. What I wanted was for each selected row in the tableview to register the row's corresponding URL. So I ended up abandoning the arrays and dictionaries ... too confusing. And going with 2 NSArrays instead. This link got me going on the right track and everything works now:
http://sweettutos.com/2012/08/25/loading-urls-from-uitableview/
In your code, two arrays stored in a dictionary , and the dictionary stored in a array.
Like this :
languageArray ----- mutedict ------ languages (FirstKey)
|
|
----------- downloadURL(SecondKey)
cell.textLabel.text is a NSString, but [[languageArray objectAtIndex:indexPath.row] objectForKey:#"FirstKey”]; is NSArray
So your code throw an error.
If your want put a language as cell.textLabel.text
You could modify your code as cell.textLabel.text = [[mutedict objectForKey:#"FirstKey"] objectAtIndex:indexPath.row]
Hope it can help you :)
Related
maybe somebody can help me. I Have different class objects. They'll get created in the TableView.m file with arrays as properties of these objects. How can I populate the TableView out of these Objects so that every cell contains on object? When I am adding the objects to an array I am getting an error, when I am running the simulator.
Error:
App1[6354:7304461] -[Object1 isEqualToString:]: unrecognized selector sent to instance 0x600000455c00
I think that the TableView doensn't know how to create the cell. But I don't have enough experience to know what is really going on.
Thanks
- (void)viewDidLoad {
[super viewDidLoad];
//creating first Object of NSObject Class Object1
Object1 *object1Class = [[WLAN alloc]init];
object1Class.object1PicsCreated = [[NSMutableArray alloc] init];
object1Class.object1TextsCreated = [[NSMutableArray alloc] init];
[object1Class.object1PicsCreated addObject:object1Class.picsObject1];
[object1Class.object1TextsCreated addObject:object1Class.textsObject1];
//creating second Object of NSObject Class Object2
Object1 *object1Class = [[WLAN alloc]init];
object2Class.object2PicsCreated = [[NSMutableArray alloc] init];
object2Class.object2TextsCreated = [[NSMutableArray alloc] init];
[object1Class.object2PicsCreated addObject:object2Class.picsObject2];
[object1Class.object2TextsCreated addObject:object2Class.textsObject2];
if (!_objects){
_objects = [[NSMutableArray alloc] init];
}
[_objects addObject:object1Class]
[_objects addObject:object2Class]
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = [_objects objectAtIndex:indexPath.row];
return cell;
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%#",[_objects objectAtIndex:indexPath.row]];
Xcode crashes because you pass an instance of Object1 to the setter which expects NSString. UITableView thinks that object is NSString so calls NSString's methods against it (for you it was -isEqualToString:), which is a reason it crashes.
Whatever you want to show needs to be converted to NSString before you pass it there.
How can I populate the TableView out of these Objects so that every cell contains on object?
This is a very strange question. UITableView's rows are UITableViewCell-s. You may not populate a table with anything else.
Maybe, you should describe your tasks in more details.
NSMutableDictionary *persondict=[[NSMutableDictionary alloc]init];
[persondict setObject:fullName forKey:#"givenName"];
[persondict setObject:identity forKey:#"identifier"];
[persondict setObject:_mobnumberarray forKey:#"CNLabelPhoneNumberMobile"];
[persondict setObject:_iphoneArray forKey:#"CNLabelPhoneNumberiPhone"];
[_Contacts addObject:persondict];
Here above is my dictionary populated with two arrays, but when I try to display the _Contacts array on tableview it gives me following error on the console screen:
[__NSArrayM length]: unrecognized selector sent to instance
0x7fd4d15211d0
Here is my table view method:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return _Contacts.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"tableViewCell";
tableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.celll.text = [[_Contacts objectAtIndex:indexPath.row]valueForKey:#"givenName"];
cell.idcell.text=[[_Contacts objectAtIndex:indexPath.row]valueForKey:#"identifier"];
cell.numbercell.text=[[_Contacts objectAtIndex:indexPath.row]valueForKey:#"CNLabelPhoneNumberMobile"];
cell.iphonecell.text=[[_Contacts objectAtIndex:indexPath.row]valueForKey:#"CNLabelPhoneNumberiPhone"];
return cell;
}
Any help will be appreciated. Thanks in advance.
If _Contact is of type NSMutableArray then try this, else replace the type:
_Contact = [[NSMutableArray alloc] init];
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];
I'm trying to get a JSON feed from Instagram to show data in my UITableView. However I get the following error and I can't figure out what the problem is:
2013-08-18 10:20:37.361 ttoauth[38213:c07] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x75d7480
2013-08-18 10:20:37.362 ttoauth[38213:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x75d7480'
*** First throw call stack:
(0x13c8012 0x11ede7e 0x14534bd 0x13b7bbc 0x13b794e 0x3f68 0x1ec8fb 0x1ec9cf 0x1d51bb 0x1e5b4b 0x1822dd 0x12016b0 0x26abfc0 0x26a033c 0x26a0150 0x261e0bc 0x261f227 0x261f8e2 0x1390afe 0x1390a3d 0x136e7c2 0x136df44 0x136de1b 0x22c57e3 0x22c5668 0x131ffc 0x2a4d 0x2975)
libc++abi.dylib: terminate called throwing an exception
Here is my code:
-(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 [jsonResults count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *instafeed_tableview = [jsonResults objectAtIndex:indexPath.row];
NSString *username_label = [[instafeed_tableview objectForKey:#"from"] valueForKey:#"username"];
cell.username.text = [NSString stringWithFormat:#"%#", username_label];
cell.username.clipsToBounds = YES;
cell.contentView.clipsToBounds = NO;
return cell;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(CGFloat)tableView :(UITableView *)tableView heightForRowAtIndexPath :(NSIndexPath *)indexPath {
return 191;
}
What am I doing wrong?
reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x75d7480
This line tells you that you are using objectAtIndex (NSArray method) to NSDictionary. The error couse becouse what you receive is NSDictionary and you want to use it like NSArray.
Probably your error couse line is:
NSDictionary *instafeed_tableview = [jsonResults objectAtIndex:indexPath.row];
Try to debug it. I think your jsonResults is NSDictionary not NSArray.
Add breakpoint to this line and in output type: po [jsonResults class];.
EDIT based on comment
You got this wrong. This not you who setting what data type do you want from JSON. JSON structure determines that. For example:
[
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
This example retturns NSArray contains NSDictionaries in it.
{
"name":"John" ,
"surname":"Doe"
},
This will return single NSDictionary
This [] and this {} brackets determines what object are you dealing with.
When i scroll my UITableView down and then scrolling up, the app crashes with the stack below:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2147483647 beyond bounds [0 .. 48]'
I know it's saying that i am accessing a cell index that exceed the UITableView size, but i am not able to figure out how to fix it. this may be my relevant code:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"CheckedTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSDictionary *rowData = [self.tableData objectAtIndex:[self tableIndexFromIndexPath:indexPath]];//this line may be the source of the crash
cell.textLabel.text = [rowData objectForKey:kCellTextKey];
if ([[rowData objectForKey:kCellStateKey] boolValue]) {
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"checked.png"]];
cell.accessoryView = imageView1;
} else {
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"unchecked.png"]];
cell.accessoryView = imageView2;
}
return cell;
}
EDIT:
Here is my tableIndexFromIndexPath method implementation
- (NSUInteger)tableIndexFromIndexPath:(NSIndexPath *)indexPath {
// Get list of items at selected section
NSArray *listData = [tableContents objectForKey:[sortedKeys objectAtIndex:indexPath.section]];
// Get name of selected row within the section
NSString *textKey = [listData objectAtIndex:indexPath.row];
// Look up that name in the tableData array
for (int i=0; i < [tableData count]; i++) {
NSDictionary *dict = [tableData objectAtIndex:i];
if ([[dict objectForKey:kCellTextKey] isEqualToString:textKey]) {
return i;
}
}
//In case Name was not found
return NSNotFound;
}
Change
NSDictionary *rowData = [self.tableData objectAtIndex:[self tableIndexFromIndexPath:indexPath]];
to
NSDictionary *rowData = [self.tableData objectAtIndex:indexPath.row];
As several have noted, your tableIndexFromIndexPath: is incorrect. Specifically it's returning NSNotFound, which suggests your using something like indexOfObject: for an object that is not in the array.
At first check that are you getting all value of your NSMutableArray?
If you are getting then check your number of section of tableView is equal to number of NSMutableArray array or not?
If same then there is a reload problem then reload your table view when you are inserting data into the NSMutableArray.
I hope you will find your solution among them.