I have an NSMutableArray which is empty at first but eventually gets filled with objects (NSString) recieved through WiFi and through the users input. This array is shown in a tableView. Pretty staight forward so far...
The problem is that I need distinguish the objects im my array recieved through WiFi from the ones recieved through the users input in order to diplay them differently in the tableView.
Example:
===============
My recieved object
===============
My inputted object
===============
My recieved object
===============
My recieved object
===============
My inputted object
===============
and so on....
How can I do this? I thought about tagging them at first but im not sure if you can tag objects in an array.
Thanks alot for your help!
create two different class objects
@interface MyReceivedObject;
@interface MyInputtedObject;
You can add both to your NSMutableArray at the appropriate times. That is, when you recieve something from Wifi, you add a new MyRecievedObject initialized with all necessary info. The same goes for when you receive user input, i.e. you add a MyInputtedObject.
Now, in your tableview, in cellForRowAtIndexPath, you can check the type and do whatever
Code:
-(UITableViewCell*)cellForRow...
{
//...deque a cell or create a new one
//..
id obj = [theArray objectAtIndex: indexPath.row];
if( [obj isKindOfClass: [MyReceivedObject class]] )
{
//set cell text color, etc for receivedtypes
}
else if ( [obj isKindOfClass: [MyInputtedObject class]])
{
//set cell text color, etc for inputted types
}
}
EDIT:
Alternatively, if the MyRecievedObject and MyInputtedObject contain identical information, then it maybe easier to create one object, and give it a "type".
This creates two subclasses of NSString that differ only in their class name. You may even be able to leave off the implementation blocks, since they are null. I haven't tried it. But it can't hurt to put them in.
This creates two subclasses of NSString that differ only in their class name. You may even be able to leave off the implementation blocks, since they are null. I haven't tried it. But it can't hurt to put them in.
Robert Scott
Ypsilanti, Michigan
Thants what I tried but I always get this error message:
Quote:
[Session started at 2009-09-09 20:51:02 +0200.]
GNU gdb 6.3.50-20050815 (Apple version gdb-966) (Tue Mar 10 02:43:13 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 65921.
warning: .o file "/Users/Basti/Desktop/Texter/build/Texter.build/Release-iphonesimulator/Texter.build/Objects-normal/i386/AppController.o" more recent than executable timestamp
kill
error while killing target (killing anyway): warning: error on line 1987 of "/SourceCache/gdb/gdb-966/src/gdb/macosx/macosx-nat-inferior.c" in function "macosx_kill_inferior_safe": (os/kern) failure (0x5x)
quit
Its hard to tell you much about that error without seeing more code.
Not sure why you're getting errors trying to extend NSString. Seems odd, but I'm not at my mac right now so i cant test it out. Instead, you could do this:
Code:
typedef enum _StringType
{
Input, Received
} StringType;
@interface MyObject: NSObject
{
NSString* theString;
StringType type;
}
@property (nonatomic, readonly) NSString* theString;
@property (readonly) StringType type;
-(id) initWithString:(NSString*) aString andType:(StringType)type;
@end
@implementation
//im sure you can figure out what goes here.
@end
//when you recieve something over wifi
MyObject* obj = [[MyObject alloc] initWithString: @"whatever was received" andType: Received ];
[myArrayOfObjects addObject: obj];
[obj release];
//when something new is input by the user
MyObject* obj = [[MyObject alloc] initWithString: @"whatever was input" andType: Input];
[myArrayOfObjects addObject: obj];
[obj release];
//in your table
-(UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell;
//...
MyObject* obj = [myArrayOfObject objectAtIndex:indexPath.row];
if( obj.type == Received )
{
cell.textColor = [UIColor redColor];
}
else if( obj.type == Input )
{
cell.textColor = [UIColor greenColor];
}
cell.text = obj.theString;
}
Last edited by smithdale87; 09-09-2009 at 03:52 PM.
2009-09-09 22:09:21.892 Texter[67536:20b] *** NSInvocation: warning: object 0xb880 of class 'MyObject' does not implement doesNotRecognizeSelector: -- abort
I don't know what the problem is. Im doing it exactly like you showed me!
//when something new is input by the user
MyObject* obj = [[MyObject alloc] initWithString:input.text andType: Input];
[services addObject: obj];
[obj release];
Quote:
//when you recieve something over wifi
MyObject* obj = [[MyObject alloc] initWithString: output andType: Received ];
[services addObject: obj];
[obj release];
error: object cannot be set - either readonly property or no setter found
I hope that is correct!?
===========================
And now I have a different problem. I Use a UILabel in my cell to diplay text. I resize the cell automatically to fit the text length and now im getting this error:
Quote:
2009-09-09 23:01:51.082 Texter[68905:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[MyObject sizeWithFont:constrainedToSize:lineBreakMode:]: unrecognized selector sent to instance 0xd19810'
Quote:
@property (nonatomic, readonly) NSString* theString;
to
Quote:
@property (nonatomic, retain) NSString* theString;
Otherwise it will not compile du to this error:
Quote:
error: object cannot be set - either readonly property or no setter found
I hope that is correct!?
That works fine as well.
Quote:
And now I have a different problem. I Use a UILabel in my cell to diplay text. I resize the cell automatically to fit the text length and now im getting this error:
Quote:
2009-09-09 23:01:51.082 Texter[68905:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[MyObject sizeWithFont:constrainedToSize:lineBreakMode:]: unrecognized selector sent to instance 0xd19810'
Quote:
CGSize constraintSize;
constraintSize.width = 200.0f;
constraintSize.height = MAXFLOAT;
CGSize stringSize = [cellValue sizeWithFont: [UIFont systemFontOfSize: 12] constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
Look at what the error is saying, the method -[MyObject sizeWithFont:constrainedToSize:lineBreakMode:] does not exist in the MyObject class.