myClass copyWithZone unrecognized selector sent to instance - ios

When trying to get a object from an mutable array to et the values for that row in a tableview I get the error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[KCNote copyWithZone:]:
unrecognized selector sent to instance 0x15589f60'
Below is my code, I had a look about this error and most places talk about issues copying data structures from the object to the new one however the objects contents is just intergers and strings as you can see below.
KCNote.h
#import <Foundation/Foundation.h>
#interface KCNote : NSObject
#property (nonatomic) NSInteger NoteID;
#property (nonatomic, strong) NSString *Note;
#property (nonatomic, strong) NSString *Author;
#property (nonatomic, strong) NSString *Date;
#end
Code in my view controller for the table.
NSMutableArray *Notes;
- (void)viewDidLoad
{
Notes = [[NSMutableArray alloc] init];
...
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"notecell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
// Configure the cell...
KCNote *note = [Notes objectAtIndex:indexPath.row];
UILabel *Author =(UILabel *) [cell viewWithTag:1];
UILabel *Date = (UILabel *) [cell viewWithTag:2];
UILabel *Note = (UILabel *) [cell viewWithTag:3];
Author.text = note.Author;
Date.text = note.Date;
Note.text = note.Note;
return cell;
}
Notes are added to the array using:
-(void)getNotes:(NSInteger)courseid{
[Notes removeAllObjects];
FMResultSet *s;
s = [db executeQuery:#"SELECT * FROM notes WHERE CourseID =?;", [NSString stringWithFormat:#"%d", courseid]];
//s = [db executeQuery:#"SELECT * FROM notes;"];
if ([db hadError]) {
NSLog(#"DB Error %d: %#", [db lastErrorCode], [db lastErrorMessage]);
}
while ([s next]) {
NSInteger NoteID = [s intForColumnIndex:0];
NSString *Note = [s stringForColumnIndex:2];
NSString *Author = [s stringForColumnIndex:3];
NSString *date = [s stringForColumnIndex:4];
KCNote *note = [KCNote new];
note.NoteID = NoteID;
note.Note = note;
note.Author = Author;
note.Date = date;
[Notes addObject:note];
}
[self.tbl_notes reloadData];
}

Related

Update UI table View from a Static method

I am developing a chat application. Some of my classes are singleton therefore I have use lot of static methods.
When ever a new message is received in app delegate. It should send it to my incomingChat viewController.
I am able to get the new message to static method in viewcontroller. But I cant reload the table from static method.
InCommingVC.h
#import <UIKit/UIKit.h>
#interface InCommingVC : UIViewController
#property (weak, nonatomic) IBOutlet UINavigationBar *navigationBarTitle;
#property (weak, nonatomic) IBOutlet UITableView *incommingTable;
+ (void) sendIncommingChats:(NSDictionary *) chatDetails;
+ (void) recieveIncomingChat:(NSDictionary *) chatDetails;
#end
InCommingVC.m
#import "InCommingVC.h"
#import "AppDelegate.h"
#import "IncommingItemObject.h"
static NSMutableArray *incomminglist;
#interface InCommingVC (){
AppDelegate *delegate;
}
#end
#implementation InCommingVC
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationBarTitle.topItem.title = #"Incomming Chats";
}
+ (void) recieveIncomingChat:(NSDictionary *) chatDetails{
NSLog(#"GOT A NEW recieveIncomingChat");
NSString *CompanyId = [chatDetails objectForKey:#"CompanyId"];
NSString *ConnectionId = [chatDetails objectForKey:#"ConnectionId"];
NSString *CountryCode = [chatDetails objectForKey:#"CountryCode"];
NSString *Department = [chatDetails objectForKey:#"Department"];
NSString *Name = [chatDetails objectForKey:#"Name"];
NSString *StartTime = [chatDetails objectForKey:#"StartTime"];
NSString *TimeZone = [chatDetails objectForKey:#"TimeZone"];
NSString *VisitorID = [chatDetails objectForKey:#"VisitorID"];
NSString *WidgetId = [chatDetails objectForKey:#"WidgetId"];
NSLog(#"------------------------------------------------------------------------------");
NSLog(#"CompanyId : %#" , CompanyId);
NSLog(#"ConnectionId : %#" , ConnectionId);
NSLog(#"CountryCode : %#" , CountryCode);
NSLog(#"Department : %#" , Department);
NSLog(#"Name : %#" , Name);
NSLog(#"StartTime : %#" , StartTime);
NSLog(#"TimeZone : %#" , TimeZone);
NSLog(#"VisitorID : %#" , VisitorID);
NSLog(#"WidgetId : %#" , WidgetId);
NSLog(#"------------------------------------------------------------------------------");
IncommingItemObject *item = [[IncommingItemObject alloc] init];
[item setCompanyId:CompanyId];
[item setConnectionId:ConnectionId];
[item setCountryCode:CountryCode];
[item setDepartment:Department];
[item setName:Name];
[item setStartTime:StartTime];
[item setTimeZone:TimeZone];
[item setVisitorID:VisitorID];
[item setWidgetId:WidgetId];
if (incomminglist.count == 0) {
incomminglist = [[NSMutableArray alloc] init];
[incomminglist addObject:item];
[[InCommingVC incommingTable] reloadData];
} else {
[incomminglist addObject:item];
}
NSLog(#"count %i", incomminglist.count);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return incomminglist.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = #"identify_incomming";
UITableViewCell *cell = [self.incommingTable dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
IncommingItemObject *item = [incomminglist objectAtIndex:indexPath.row];
UIImageView *CountryImage = (UIImageView *)[cell viewWithTag:5010];
[CountryImage setImage:[UIImage imageNamed:item.CountryCode]];
UILabel *visitorName = (UILabel *)[cell viewWithTag:5011];
visitorName.text = item.Name;
UILabel *visitStartTime = (UILabel *)[cell viewWithTag:5012];
visitStartTime.text = item.StartTime;
return cell;
}
I want to update my incommingTable from a static method. can some one help me. tnx.
I am having this error
/Users/zupportdesk/Desktop/MyIOSApps/Chat System/Chat
System/InCommingVC.m:96:23: No known class method for selector
'incommingTable'
while doing
[[InCommingVC incommingTable] reloadData];
2 ways :
1 - Make a shared instance. Call :
[[[self class] sharedInstance].tableView reloadData];
2 - Make you class confirm to some notification , that you'll send upon receiving message with payload (chat dictionary). Make sure to deregister the notification when view controller de-allocates
[self.incommingTable reloadData];
Try this please and make sure IBOUTLET is proper connected

reorder and save UITableView

I parse data from a server which contains passengers details.
the passengers details are saved into Passenger class.
The main viewController contains a UITableView which loads the passengers data and allows the user to rearrange the cells order.
My question is how can I save the tables new order and load it again each time the app starts, but with new passenger details parsed from the server.
I prefer not use core data.
Here is the code:
Passenger.h
#import <Foundation/Foundation.h>
#interface Passenger : NSObject
{
NSString *name;
NSString *code;
NSString *country;
NSString *date;
}
#property (nonatomic, strong) NSString *name;
#property (nonatomic, strong) NSString *code;
#property (nonatomic, strong) NSString *country;
#property (nonatomic, strong) NSString *date;
mainViewController m
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSString *stringToMove = passengersArray[sourceIndexPath.row];
[passengersArray removeObjectAtIndex:sourceIndexPath.row];
[passengersArray insertObject:stringToMove atIndex:destinationIndexPath.row];
}
- (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];
}
Passenger *current = [passengersArray objectAtIndex:indexPath.row];
UILabel *nameLabel = (UILabel *)[cell viewWithTag:101];
nameLabel.text = current.name;
UILabel *codeLabel = (UILabel *)[cell viewWithTag:102];
codeLabel.text = current.code;
UILabel *countryLabel = (UILabel *)[cell viewWithTag:103];
countryLabel.text = current.country;
return cell;
}
You can load the passenger list from a plist in form of an Array. And when you update the information you iterate through that plist array, compare, update and add new passengers to the list. Once the user has rearranged the order you save it to the plist.
Fetch
NSPropertyListFormat format;
NSString *errorDesc;
NSString * plistPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [plistPath stringByAppendingPathComponent:#"Passengers.plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSArray *temp = (NSArray *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
Save
NSString *error;
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:#"Passengers.plist"];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:_passengersArray format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData)
{
[plistData writeToFile:plistPath atomically:YES];
}
else
{
NSLog(#"Error writing passengers to file %#", error);
}

get crashed when deference subclassed NSObject

#interface PADiscover : NSObject
#property (nonatomic, assign) unsigned int dw3C;
#property (nonatomic, assign) BOOL isSet;
#property (nonatomic, assign) PAContactModel model;
#end
In another VC
#property (nonatomic, strong) NSMutableDictionary *shakeDict;
Then I add some objects to shakeDict
- (void)viewDidLoad {
NSNumber *num = [NSNumber numberWithUnsignedInt:message.sDeviceInfo.dw3CId];
PADiscover *discover = [[PADiscover alloc] init];
discover.dw3C = message.sDeviceInfo.dw3CId;
discover.model = message.sDeviceInfo.dwDeviceType;
discover.isSet = message.sDeviceInfo.fgPasswdFlag;
[_shakeDict setObject:discover forKey:[NSString stringWithFormat:#"%#", num]];
[_shakeTV reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
PADiscover *discover = [[_shakeDict allKeys] objectAtIndex:indexPath.row];
**cell.textLabel.text = [NSString stringWithFormat:#"ID %u, ISSet %d", discover.dw3C, discover.isSet];//crashed!**
cell.detailTextLabel.text = [NSString stringWithFormat:#"model %d", discover.model];
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:11];
return cell;
}
It seems cannot deference PADiscover successfully,but I'm not sure when place I get some wrong.
can anyone explain that for me?
Try this,
NSString *key = [[_shakeDict allKeys] objectAtIndex:indexPath.row];
PADiscover *discover = [_shakeDict objectForKey:key];
instead of
PADiscover *discover = (PADiscover *)[[_shakeDict allKeys] objectAtIndex:indexPath.row];
At least this line may be error:
#property (nonatomic, assign) PAContactModel model;
change to strong if a model class.
#property (nonatomic, strong) PAContactModel model;

NSInvalidArgumentException - unrecognized selector sent to instance

I am using the following function to do a search using a SearchDisplayController
- (void)handleSearchForTerm:(NSString *)searchTerm{
[self.searchResults removeAllObjects]; // First clear the filtered array.
FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
[db open];
FMResultSet *results = [db executeQuery: [NSString stringWithFormat:#"SELECT * FROM periods where (searchstartyear <= %#) AND (searchendyear >= %#)", searchTerm, searchTerm]];
NSMutableArray *array = [[NSMutableArray alloc] init ];
[array removeAllObjects];
while ([results next]) {
Periods *searchPeriod = [[Periods alloc] init];
searchPeriod.periodname = [results stringForColumn:#"PeriodName"];
searchPeriod.startyear = [results stringForColumn:#"StartYear"];
searchPeriod.endyear = [results stringForColumn:#"EndYear"];
searchPeriod.thumb = [results stringForColumn:#"Thumb"];
searchPeriod.childid = [results stringForColumn:#"ChildID"];
[array addObject:searchPeriod];
}
[self.searchResults addObject:array];
[db close];}
Periods is an object
#import <UIKit/UIKit.h>
#interface Periods : NSObject
#property (nonatomic,strong) NSString *periodname;
#property (nonatomic,strong) NSString *startyear;
#property (nonatomic,strong) NSString *endyear;
#property (nonatomic,strong) NSString *thumb;
#property (nonatomic,strong) NSString *childid;
#end
I get the NSInvalidArgumentException - unrecognized selector sent to instance when the following code is executed:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PeriodsCell";
UITableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
if (tableView != [[self searchDisplayController] searchResultsTableView])
{
NSMutableArray *array = [periodsdataarray objectAtIndex:indexPath.section];
NSMutableDictionary *dictionary = [array objectAtIndex:indexPath.row];
UILabel *childIDLabel = (UILabel *)[cell viewWithTag:999];
childIDLabel.text = [dictionary valueForKey:#"ChildID"];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
titleLabel.text = [dictionary valueForKey:#"PeriodName"];
UILabel *previewLabel = (UILabel *)[cell viewWithTag:101];
previewLabel.text = [NSString stringWithFormat:#"%# - %#",[dictionary valueForKey:#"StartYear"],[dictionary valueForKey:#"EndYear"]];
UIImageView *imageview = (UIImageView *)[cell viewWithTag:102];
[imageview setImage:[UIImage imageNamed:[dictionary valueForKey:#"Thumb"]]];
}
else
{
Periods *periodcell = [self.searchResults objectAtIndex:indexPath.row];
cell.tag = 666;
UILabel *childIDLabel = (UILabel *)[cell viewWithTag:999];
childIDLabel.text = periodcell.childid;
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
titleLabel.text = periodcell.periodname;
UILabel *previewLabel = (UILabel *)[cell viewWithTag:101];
previewLabel.text = [NSString stringWithFormat:#"%# - %#",periodcell.startyear,periodcell.endyear];
UIImageView *imageview = (UIImageView *)[cell viewWithTag:102];
[imageview setImage:[UIImage imageNamed:periodcell.thumb]];
}
return cell;
}
...and more specifically when this line is executed:
childIDLabel.text = periodcell.childid;
What I am doing wrong?
The error I was making was at the handleSearchForTerm function.
I was using the NSMutableArray *array to hold the values of my Periods object and then I was adding the array to my searchResults array (expecting that the searchResults was holding Periods). The solution was as simple as adding Periods directly to my searchResults array and ditching array at all...

Memory keep increasing when the UITableView scrolling

there is a strange problem I have not met ever
there is an array() including some custom object named MyClass parsed by JSONKit;
when I keep scrolling the tableview the memory will keeping increasing too.
but when replace
cell.textLabel.text = myclass.name;
with
cell.textLabel.text = #"cool";
or
cell.textLabel.text = [NSString stringWithFormate:#"a-%d", indexPath.row];
it's ok the memory with keep stable
but if I use
cell.textLabel.text = [NSString stringWithFormate:#"a-%#-i",myclass.name, indexPath.row];
it also keep increasing;
It will drive my crazy!!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Singers";
OMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
MyClass *myclass = [self.data objectAtIndex:indexPath.row];
if (cell == nil){
cell = [[[OMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
cell.textLabel.text = myclass.name;
return cell;
}
MyClass
there is two class one Base another inherit
Base:
#interface OMBase : NSObject {
NSMutableDictionary *data;
NSString *name;
NSArray *keys;
}
#property (nonatomic, retain) NSString *name;
#property (nonatomic, copy) NSMutableDictionary *data;
#implementation OMBase
#synthesize data, name;
- (void)setData:(NSMutableDictionary *)adata{
if (data){
[data release];
data = nil;
}
data = [adata mutableCopy];
}
- (void)dealloc{
if (keys){
[keys release];
}
[data release];
[super dealloc];
}
- (id)init{
if (self = [super init]){
self.data = [[[NSMutableDictionary alloc] initWithCapacity:20] autorelease];
}
return self;
}
inherit:
#import "OMBase.h"
#interface OMLyric : OMBase
- (NSString *)songid;
- (NSString *)content;
#import "OMLyric.h"
#implementation OMLyric
- (NSString *)songid{
return [data objectForKey:#"songid"];
}
- (NSString *)content{
return [data objectForKey:#"content"];
}
Seems like your myclass.name getter returns a new allocated object. We can't say more without seeing myclass.

Resources