开发者

NSMutableArray of Objects Returning Type UICGColor during cellForRowAtIndexPath Event

开发者 https://www.devze.com 2022-12-20 06:35 出处:网络
I\'m pretty new to Objective C and iPhone development and have hit a problem which has completely stumped me

I'm pretty new to Objective C and iPhone development and have hit a problem which has completely stumped me

I have a class, Sale, which has the following class method:

+(NSMutableArray*)liveSales {
NSMutableArray *liveSales = [[NSMutableArray alloc] init];

for(int i = 0; i <= 100; i++)
{
    Sale *s = [[Sale alloc] init];
    //[s setTitle:[NSString stringWithFormat:@"Sale %开发者_开发百科d", i+1]];
    [s setTitle:@"Title"];
    //[s setDescription:[NSString stringWithFormat:@"Sale %d descriptive text", i+1]];
    [s setDescription:@"Description"];

    [liveSales addObject:s];

    [s release];
    s = nil;
}

return [liveSales autorelease];

}

I'm then using this class method in the following way:

#import "RootViewController.h"
@implementation RootViewController
@synthesize liveSales = _liveSales;


- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    [[self navigationItem] setTitle:@"Sales"];

    if (![self liveSales]) 
    {
        [self setLiveSales:[Sale liveSales]];
    }

}

I then customise my table cells using the following event:

// Customize the appearance of table view cells.
- (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] autorelease];
    }

    // Configure the cell.
    Sale *sale = [[self liveSales] objectAtIndex:[indexPath row]];

    [[cell textLabel] setText:[sale title]];
    [[cell detailTextLabel] setText:[sale description]];

    [sale release];
    sale = nil;

    return cell;
}

If I then run this code using the Simulator I can see that each table cell shows the correct information and everything appears to be working correctly, that is until I scroll to the very bottom of the list and the application dies with the following information recorded in the console window:

2010-02-10 20:23:50.741 AppName[1828:207] *** -[UICGColor title]: unrecognized selector sent to instance 0x3b05750
2010-02-10 20:23:50.743 AppName[1828:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UICGColor title]: unrecognized selector sent to instance 0x3b05750'
2010-02-10 20:23:50.744 AppName[1828:207] Stack: (
    29291611,
    2510427401,
    29673531,
    29242998,
    29095618,
    8789,
    3047008,
    3013271,
    3089642,
    3053907,
    55804592,
    55804015,
    55802054,
    55801146,
    55834680,
    29078098,
    29075039,
    29072456,
    37382029,
    37382226,
    2764803
)

I've spent a good few hours trying to work out what's going on here to no avail but I'm sure I'm either missing something really obvious, or approaching the logic of the application in the wrong way

Any help anyone could provide would be greatly appreciated :)

Thanks

Dave


You make an extra release in:

// Configure the cell.
Sale *sale = [[self liveSales] objectAtIndex:[indexPath row]];

[[cell textLabel] setText:[sale title]];
[[cell detailTextLabel] setText:[sale description]];

[sale release];
sale = nil;

sale is an autoreleased instance obtained from an array, don't release it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号