How can we display number values in UILabels in iOS? - ios

I am very new for iOS and in my app I am integrating services and after getting response from service I am storing the values in Array-list.
When I load that array list values in tableList showing exception like:
_Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x7d7ca000'
McoIdArray:-(
106314100491,
106314100492,
106314100493,
106314100494,
106314100495
)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"MyCell";
Cell = (CoridersCell *)[tableList dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (Cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CoridersCell" owner:self options:nil];
Cell = [nib objectAtIndex:0];
}
Cell.mcoId.text = [McoIdArray objectAtIndex:indexPath.row];
return Cell;
}

The problem is you are assigning an NSNumber instance instead of an NSString. You should format the number to a string like this:
NSNumber* number = [McoIdArray objectAtIndex:indexPath.row];
cell.mcoId.text = [NSString localizedStringWithFormat:#"%#", number];
If you want to have more control over number formatting you can use NSNumberFormatter.

Your array contains instances of NSNumber, not NSString. So you can't directly assign the value from the array to the label's text property. You must first convert the NSNumber to an NSString.
One option is to use the stringValue method.
Cell.mcoId.text = [McoIdArray[indexPath.row] stringValue];
Also note the use of modern syntax to access the array.
Replace the line:
Cell.mcoId.text = [McoIdArray objectAtIndex:indexPath.row];
with the line I show above.

Related

UITableViewCell - Updating NSMutableDictionary Values

I have UITableViewController and it has twitter like post with like buttons. what im doing is when ever like button clicked if it is success trying to update the like count by + 1. i did the all the the above method except for the update part.
im getting Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' Error.
Here is my code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
FeedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDictionary *feedList = [liveFeeds objectAtIndex:indexPath.row];
//liveFeeds is NSMutableArray
cell.likeCount.text = [feedList objectForKey:#"likes"];
cell.like.tag = indexPath.row;
[cell.like addTarget:self action:#selector(likeClicked:) forControlEvents:UIControlEventTouchUpInside];
}
-(void) likeClicked:(UIButton*)sender{
//Here im using AFNetworking and getting JSON response.
//After that im doing following to update the like
NSMutableDictionary* feedList = [liveFeeds objectAtIndex:sender.tag];
NSString *oldLike = [feedList objectForKey:#"likes"];
int newLike = [oldLike intValue] + 1;
NSString *strFromInt = [NSString stringWithFormat:#"%d",newLike];
NSLog(#"updated like %#",strFromInt); // up to this it works
[feedList setObject:strFromInt forKey:#"likes"]; // in here it get crashe
[self.tableView reloadData];
}
What i want to do is. liveFeeds Array update with that Like Count and reload the table. am i doing this wrong? or is there any easy way to do this?
Most likely [liveFeeds objectAtIndex:sender.tag] is a NSDictionary, not an NSMutableDictionary. So you cannot change its content.
Build feedlist with the content of [liveFeeds objectAtIndex:sender.tag] :
NSMutableDictionary* feedlist = [NSMutableDictionary dictionaryWithDictionary:[liveFeeds objectAtIndex:sender.tag]]
I haven't tried it yet but i guess you're trying to use a NSMutableDictionary method on a NSDictionary.
Try changing:
NSMutableDictionary* feedList = [liveFeeds objectAtIndex:sender.tag];
To:
NSDictionary* dic = [liveFeeds objectAtIndex:sender.tag];
NSMutableDictionary* feedList = [NSMutableDictionary alloc] initWithDictionary:dic]];
Simply create mutable copy of NSDictionary. Actually NSMutableDictionary* feedList = [liveFeeds objectAtIndex:sender.tag] returns NSDictionary. So to make it editable you have to create another copy which is mutable by using mutableCopy. NSMutableDictionary* feedList = [[liveFeeds objectAtIndex:sender.tag]mutableCopy]
-(void) likeClicked:(UIButton*)sender{
//Here im using AFNetworking and getting JSON response.
//After that im doing following to update the like
NSMutableDictionary* feedList = [[liveFeeds objectAtIndex:sender.tag]mutableCopy];
NSString *oldLike = [feedList objectForKey:#"likes"];
int newLike = [oldLike intValue] + 1;
NSString *strFromInt = [NSString stringWithFormat:#"%d",newLike];
NSLog(#"updated like %#",strFromInt); // up to this it works
[feedList setObject:strFromInt forKey:#"likes"]; // in here it get crashe
[self.tableView reloadData];
}

custom UItableView not displaying correctly on ios8

I made a custom UITableViewCell and when I display it, i have this result (I'm running xcode 6 and iOS 8 beta 1 on an iPhone 5.)
http://imgur.com/2MyT0mF,Nx1o4bl#0
And when I rotate my device, then rotate back to portrait, everything becomes correct.
http://imgur.com/2MyT0mF,Nx1o4bl#1
Note that when I was using xcode 5 to compile my app, I had no problems.
Code used in cellForRowAtIndexPath:
BGTCoursCell1 *cell = (BGTCoursCell1 *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"BGTCoursCell1" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm"];
NSLocale *locale = [NSLocale currentLocale];
[dateFormatter setLocale:locale];
if (indexPath.section == 0)
{
BGTCours *cours = [[currentDay coursMatin] objectAtIndex:indexPath.row];
cell.matiereLabel.text = [cours matiere];
cell.salleLabel.text = [cours salle];
cell.heureLabel.text = [NSString stringWithFormat:#"De %# à %#", [dateFormatter stringFromDate:[cours beginDate]], [dateFormatter stringFromDate:[cours endDate]]];
}
else
{
BGTCours *cours = [[currentDay coursAprem] objectAtIndex:indexPath.row];
cell.matiereLabel.text = [cours matiere];
cell.salleLabel.text = [cours salle];
cell.heureLabel.text = [NSString stringWithFormat:#"De %# à %#", [dateFormatter stringFromDate:[cours beginDate]], [dateFormatter stringFromDate:[cours endDate]]];
}
return cell;
Thanks !
You need to return a CGFloat from the tableView:heightForRowAtIndexPath: . The error you are seeing will appear if you return a float from the same method:
//causes the bug
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
}
//fixes the bug
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return tableView.rowHeight; // whatever
}
This question has an accepted answer, but I have the same problem which was not fixed by the accepted answer which tells us to change float to CGFloat.
I found out that people who has code based on table's rowHeight may have this problem too. In iOS8 it is important to look at estimatedRowHeight not just rowHeight.
As I was using these variables in my programmatically generated UITableView, fixing those variables solved the problem.
I might be a bit late to the party but I've encountered exactly same problem with iOS 8 beta 2 and xCode 6 beta 2 today(26-June-2014), I see they still haven't fixed the bug.
There's also another bug with xCode 6 beta, that is my app can no request the GPS location properly - it simply won't ask for the permission. This is also solved by switching back to xCode 5. I think at the moment is more viable to use xCode 5 to build and test your app.
try reusing the cell, like, change:
BGTCoursCell1 *cell = (BGTCoursCell1 *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"BGTCoursCell1" owner:self options:nil];
cell = [nib objectAtIndex:0];
to
static NSString *CellIdentifier = #"Cell";
static NSString *CellNib = #"BGTCoursCell1";
BGTCoursCell1 *cell = (BGTCoursCell1 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if( cell == nil ) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = [nib objectAtIndex:0];
}
...
return cell;
i had a slightly similar problem and fixed it by using
cell.clipsToBounds = YES

Multiple methods named 'location' found with mismatched result, parameter type, or attributes

I have read the other questions concerning multiple methods but still do not know how to fix my code. I would be grateful for help with this. I have put a * around the statement where the error occurs.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"eventCell";
EQCalendarCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[EQCalendarCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.titleLabel.text = [[self.eventsList objectAtIndex:indexPath.row] title];
cell.locationLabel.text = [[self.eventsList objectAtIndex:indexPath.row] location];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat: #"dd-MM-yy HH:mm"];
NSString *startDateString = [df stringFromDate:[[self.eventsList
objectAtIndex:indexPath.row] startDate]];
cell.startDateLabel.text = startDateString;
NSString *endDateString = [df stringFromDate:[[self.eventsList
objectAtIndex:indexPath.row] endDate]];
cell.endDateLabel.text = endDateString;
return cell;
}
Thanking you in advance for your help.
Casting the result of retrieving the object from the self.eventsList collection should solve the problem. E.g.:
cell.locationLabel.text = [((MyClassName *)[self.eventsList objectAtIndex:indexPath.row]) location];
Replacing MyClassName with the name of the class in the collection.
You need to cast [self.eventsList objectAtIndex:indexPath.row] to the relevant type so that the compiler knows what data type your are dealing with.
Without seeing how your self.eventList list is populated it's not possible to tell you the solution exactly, but your line should be replaced with something like this (split into two lines for clarity, but you could use a cast instead of a variable to keep it on one line)
MyEventClass *event = [self.eventsList objectAtIndex:indexPath.row];
cell.locationLabel.text = [event location];
You need to cast EKEvent class. This will solve your problem...
EKEvent *event = [self.eventlist objectAtIndex:indexPath.row];
NSString *placeStr=[event location];
In one scenario, if the factory method that creates the object has return type "id" then the compiler will check the method signature in all the classes. If compiler find the same method signature in more than one class then it will raise the issue. So replace the return type "id" with "specific class name".

[__NSCFNumber length]: unrecognized selector sent to instance UITableView

i'm experiencing an error
[__NSCFNumber length]: unrecognized selector sent to instance 0x15580c90
2014-02-18 15:10:49.490 CIB[1706:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x15580c90'
* First throw call stack:
(0x2da18e83 0x37d756c7 0x2da1c7b7 0x2da1b0af 0x2d969dc8 0x2e33b695 0x2e33b169 0x301ab2fd 0x1603ad 0x302cf315 0x302776cd 0x30276ef1 0x3019d353 0x2fe23943 0x2fe1f167 0x2fe1eff9 0x2fe1ea0d 0x2fe1e81f 0x2fe1854d 0x2d9e3f69 0x2d9e18f7 0x2d9e1c43 0x2d94c471 0x2d94c253 0x326862eb 0x30201845 0x113de1 0x3826eab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
I have loop here. from array in Json to my model tasklist then stored to NSMutableArray _tasklist
NSArray *taskJson = [json objectForKey:#"fOTaskListModelWss"];
for (NSDictionary *dictCQ in taskJson) {
NSLog(#"TASKLIST: %#", [dictCQ objectForKey:#"foTaskListModelWs"]);
NSDictionary *datadic = [dictCQ objectForKey:#"foTaskListModelWs"];
TaskList *task = [[TaskList alloc]init];
[task setTaskCount:datadic[#"count"]];
[task setFuncCd:datadic[#"funcCd"]];
[task setFuncCdDscp:datadic[#"funcCdDscp"]];
[task setRequestStatus:datadic[#"requestStatus"]];
[task setRole:datadic[#"role"]];
[_taskList addObject:task];
}
then here is my code in cellForRowAtRowPathIndex
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * simpleTableIdentifier = #"MenuTableViewCell";
MenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
TaskList *txn = [_taskList objectAtIndex:indexPath.row];
cell.titleLabel.text = txn.funcCdDscp;
cell.totalCountLabel.text = txn.taskCount;
return cell;}
cell.titleLabel.text = txn.funcCdDscp;
cell.totalCountLabel.text = txn.taskCount;
One of these (not sure which, but my guess would be taskCount) is a NSNumber. Text takes an NSString.
cell.titleLabel.text = txn.funcCdDscp;
cell.totalCountLabel.text = [txn.taskCount stringValue];
OR
Use this as this is best solution
cell.totalCountLabel.text = [NSString stringWithFormat:#"%#",txn.taskCount];
Hope the following will help you if you are manipulating JSON data from webservice
cell.textLabel.text=[NSString stringWithFormat:#"%#",[[JsonDictionaryObject objectForKey:#"Respected_Key_Name"]objectAtIndex:indexPath.row]];
Here it is expecting NSString instead of NSNumber so that is the reason it crashes.So convert that to NSString then it will be resolved.

Error in string length

I have a table view which may contain number of data.
When user select one particular cell I set Id in string based on that selection. And then I am trying to find length of that string. But I am getting error in that. When I print that string it print its value.
Here is my code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *charityArray = [dataArray objectAtIndex:indexPath.row];
charityId=[charityArray objectAtIndex:0];
NSLog(#"charityId%#",charityId);
NSLog(#"charitylen%d",[charityId length]);
}
and here is log screen.
2013-12-04 14:47:13.897 GratZeez[2178:a0b] charityId3
2013-12-04 14:47:13.897 GratZeez[2178:a0b] -[__NSCFNumber length]: unrecognized selector sent to instance 0xd174070 2013-12-04 14:47:13.899
GratZeez[2178:a0b] * Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFNumber length]:
unrecognized selector sent to instance 0xd174070'
declaration of charityId:
#import <UIKit/UIKit.h>
#interface ExsitingCharityViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
NSMutableArray *dataArray,*tempArray,*searchResultArray,*nameSearchArray;
BOOL search;
NSMutableDictionary *dataDict;
CGPoint originalCenter;
NSString *charityId;
}
try this code, Convert NSNumber to NSString and then use length
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *charityArray = [dataArray objectAtIndex:indexPath.row];
charityId = [charityArray objectAtIndex:0];
NSString *charityString = [charityId stringValue];
NSLog(#"charityString = %#", charityString);
NSLog(#"charityString length = %d",[charityString length]);
}
As you post, charityId s not a NSNumber formatted type value.
If you want to know to length convert in to NSString formate,
Like, NSString* str = [NSString stringwithFormat:#"%#", charityId];
then you get length.
As the error log shows, you are sending the length message to a NSNumber object. According to your post, you seem to be trying to send it to a NSString but it is not a NSString.
-[__NSCFNumber length]: unrecognized selector sent to instance 0xd174070'
If you wish to use the %d string format specifier with an NSNumber you should convert the NSNumber object to an integer first:
NSLog(#"charityId%d",[charityId intValue]);
use this code for NSNumber to NSString
NSString *strId=[NSString StringWithFormat#"%d", charityId];
NSLog(#"charitylen%d",[strId length]);
If you are trying to NSLog an NSNumber do the following, replace this:
NSLog(#"charitylen%d",[charityId length]);
With this:
NSString *myString = [charityId stringValue];
NSLog(#"charitylen%#", myString);
And if you are trying to get the count of your Array use the following:
NSLog(#"charitylen%d",[charityId count]);
Update:
I have to opinions about your case:
1- It means that you are passing an NSNumber where the called code expects an NSString or some other object that has a length method. You can tell Xcode to break on exceptions so that you see where exactly the length method gets called.
2-I think it may because your charityId is empty or never used before you NSLog it .
-[__NSCFNumber length]: unrecognized selector sent to instance it's mean you are tried to get length of a number (i.e. int, NSInteger etc). so you need to typecast.
you can do it either using [NSString stringWithFormat:#"%d", charityId] or [charityId stringValue]
so just do below
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *charityArray = [dataArray objectAtIndex:indexPath.row];
charityId=[charityArray objectAtIndex:0];
NSString *charityIdString=[NSString stringWithFormat:#"%d", charityId];
NSLog(#"charityId%#",charityIdString);
NSLog(#"charitylen%d",[charityIdString length]);
}

Resources