Question

EDIT: Thanx to user2404543, I have found my mistake. Code is edited to show the fix.

I have a tableViewController that sets up a table with a list of states. I am trying to set its cell.background color using a UIColor that is part of an object in an NSMutableArray. The object consists of an NSString stateName, an NSString stateNickName, an NSString licensePlateURL, and a UIColor color. All of the elements of the object populate the table as expected, however, when I try to set the cell.backgroundColor using the "color" element from the object, I get an error. Here is the pertinent code:

The stateObject.h has this information:

@interface stateObject : NSObject

@property (strong,nonatomic) NSString *stateName;
@property (strong,nonatomic) NSString *nickName;
@property (strong,nonatomic) NSString *licensePlateURL;
@property (strong,nonatomic) UIColor *bkgColor;

-(void) createState:(NSString*) name nickName:(NSString*) nName licenseURL:(NSString*) url color:(UIColor*) color;

@end

and here is the stateObject.m file info:

@synthesize stateName,nickName,licensePlateURL,bkgColor;

-(void) createState:(NSString *)name nickName:(NSString *)nName licenseURL:(NSString *)url color:(UIColor *)color
{
    stateName = name;
    nickName = nName;
    licensePlateURL = url;
    bkgColor = color;

}

The object is set up in my dataSource class and added to my NSMutableArray, the object looks like this:

stateObject *newState1 = [[stateObject alloc] init];
[newState1 createState:@"Alabama" nickName:@"Yellowhammer State" licenseURL:@"http://www.15q.net/us1/al09a.jpg" color:[UIColor whiteColor]];
alabama = newState1; 

The method I am using is this:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{//This line is now correct and properly sets the backgroundColor.
    cell.backgroundColor = [self.stateArray[indexPath.section][indexPath.row]bkgColor];
}

The error I get is this:

assignment2[11591:c07] -[stateObject color]: unrecognized selector sent to instance 0x759b620
assignment2[11591:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[stateObject color]: unrecognized selector sent to instance 0x759b620'

If I just create a UIColor object in said method and then set the value to that object, it works fine, so what am I doing wrong?

The whole reason I need to use the UIColor from the array is so the cells will retain their color even after I scroll them off the screen. They do not do so if I just set the color in the

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

method.

Was it helpful?

Solution

The error message states that your stateObject can't respond to the message "color". In other words your class stateObject do not have a method named "color".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top