开发者

setting UITableViewCell to value from custom object instance

So basically I am trying to create instances of the class below every time I received a valid response from a web request and then store those instances in an array so I can access their data later. Then, I try to populate a table view with specific fields from the instance(s) that are stored in the array. I've been having some issues since I am very familiar with C++ and do this sort of thing with vectors and then just access based off of the index I need, but this has had me pulling my hair out! Thanks, code is below:

eventDetails.h:

@interface eventDetails : NSObject {
NSString *eventName, *eventID;
}

-(void) setEventID : (NSString *) ID;
-(void) setEventName: (NSString *) name;
-(NSString *) getEventName;
-(NSString *) getEventID;

and also note that

 NSMutableArray *events

is declared in my .h file and

 events = [[NSMutableArray alloc] init]; 

has been called in the viewDidLoad

I then dynamically create instances as a response is received from an web request and add them to an array:

if ([elementName isEqualToString:@"id"])
{
    NSLog(@"at beginning of event, length is %i", [events count]);
    temp = [[eventDetails alloc] init];
    [temp setEventID:[NSMutableString stringWithString:soapResults]];

    [soapResults setString:@""];
    elementFound = FALSE; 
}

if ([elementName isEqualToString:@"name"])
{

    [temp setEventName:[NSMutableString stringWithString:soapResults]];
    [events addObject:temp];
    [soapResults setString:@""];
    elementFound = FALSE; 
    //[temp release];

}

After everything is all said and done, I created a little test function to ensure the data was set correctly:

-(void) test{
for (eventDetails *s in events){
    NSLog(@"Entry ID: %@ with name %@", [s getEventID], [s getEventName]);
 }
}

and I get the following (correct) output:

2011-04-09 18:53:24.624 Validator[90982:207] Entry ID: 701 with name iPhone Test Event 2011-04-09 18:53:24.625 Validator[90982:207] Entry ID: 784 with name Another iPhone Test Event 2011-04-09 18:53:24.626 Validator[90982:207] Entry ID: 839 with name third iphone

I then try to refresh the table view, and have it pull in data from the instances in the array:

 // Customize开发者_如何学编程 the appearance of table view cells.
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
//---try to get a reusable cell---
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//---create new cell if no reusable cell is available---
if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                  reuseIdentifier:CellIdentifier] autorelease];
}
//---set the text to display for the cell---
eventDetails *cellDetails = [[eventDetails alloc] init];
NSInteger row = [indexPath row];
cellDetails = [[self events] objectAtIndex:row];

NSString *cellValue = [cellDetails getEventName];
NSLog(@"Event is: %@", cellValue);
cell.textLabel.text = cellValue;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;

}

But every time the program gets to this part, it crashed which a EXC_BAD_ACCESS where I say:

 cell.textLabel.text = cellValue;

Thanks for your help. I think I might be doing something wrong with how I declare the instances of the eventDetails class, but I am not sure since it is working correctly as far as storing that data. If you need any more code, I have the missing sections.


There are too many omitted details in the code you posted to know for sure, but my guess would be the eventName isn't retained, and is deallocated sometime before you attempt to use it.

Check your setEventName: implementation; it would need to send either retain or copy to the name argument to ensure that the string won't be deallocated before you're done using it. However, the situation is more complex than that if you want to avoid memory leaks, so you if you haven't done so already, I'd recommend reading up on memory management, in particular, Apple's excellent Memory Management Programming Guide. (Note: I've given up posting links since Apple keeps changing them).

A side note: don't prefix the names of accessor methods with the word get; that would be fine in Java or C++, but this is Objective-C. Your accessors should look like this:

- (NSString *)eventName;
- (NSString *)eventID;

There's no guarantee that Foundation mechanisms that rely on introspection will work correctly with accessors that don't follow the documented naming conventions, so that's another thing to read up on. :-)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜