Cell Not Being Added To TableViewController
My app is crashing (freezing) when I click the add button on my uialertview and it is supposed to add a cell to the table. The error is related to the line of code I posted here:
Here is the error message:
SIGABRT at this line: cell.textLabel.text = [self.eventsArray objectAtIndex:indexPath.row];
Ok i figured out the problem is because eventArray holds an object rather than the string I need. How can I modify my code so that the save/fetch works correctly?
What is supposed to happen is that when the user enters a string into the alert view and presses ok, it is supposed to save into the name attribute of the routine entity and the table view should update itself accordingly.
Here is my viewController Code:
#import "RoutineTableViewController.h"
#import "AlertPrompt.h"
#import "Routine.h"
#import "CurlAppDelegate.h"
@implementation RoutineTableViewController
@synthesize tableView;
@synthesize eventsArray;
@synthesize managedObjectContext;
- (void)dealloc
{
[managedObjectContext release];
[eventsArray release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)addEvent
{
CurlAppDelegate *curlAppDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [curlAppDelegate managedObjectContext];
Routine *routine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:context];
NSManagedObject *newRoutineEntry;
newRoutineEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:context];
NSError *error = nil;
if (![context save:&error]) {
// Handle the error.
}
NSLog(@"%@", error);
[eventsArray insertObject:routine atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
CurlAppDelegate *curlAppDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [curlAppDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Routine" inManagedObjectContext:context];
[request setEntity:entity];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
[self setEventsArray:mutableFetchResults];
[mutableFetchResults release];
[request release];
UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showPrompt)];
[self.navigationItem setLeftBarButtonItem:addButton];
[addButton release];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEdit)];
self.navigationItem.rightBarButtonItem = editButton;
[editButton release];
[super viewDidLoad];
}
-(void)toggleEdit
{
[self.tableView setEditing: !self.tableView.editing animated:YES];
if (self.tableView.editing)
[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
else
[self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
}
-(void)showPrompt
{
AlertPrompt *prompt = [AlertPrompt alloc];
prompt = [prompt initWithTitle:@"Add Workout Day" message:@"\n \n Please enter title for workout day" delegate:self cancelButtonTitle:@"Cancel" okButtonTitle:@"Add"];
[prompt show];
[prompt release];
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex])
{
NSString *entered = [(AlertPrompt *)alertView enteredText];
if(eventsArray && entered)
{
[eventsArray addObject:entered];
[tableView reloadData];
[self addEvent];
}
}
}
- (void)viewDidUnload
{
self.eventsArray = nil;
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [eventsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellEditingStyleDelete reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = [self.eventsArray objectAtIndex:indexPath.row];
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
Here is console output:
2011-03-30 02:31:44.725 Curl[3303:707] -[Routine isEqualToString:]: unrecognized selector sent to instance 0x19d650
2011-03-30 02:31:44.824 Curl[3303:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Routine isEqualToString:]: unrecognized selector sent to instance 0x19d650'
*** Call stack at first throw:
(
0 CoreFoundation 0x3234564f __exceptionPreprocess + 114
1 libobjc.A.dylib 0x36588c5d objc_exception_throw + 24
2 CoreFoundation 0x323491bf -[NSObject(NSObject) doesNotRecognizeSelector:] + 102
3 CoreFoundation 0x32348649 ___forwarding___ + 508
4 CoreFoundation 0x322bf180 _CF_forwarding_prep_0 + 48
5 UIKit 0x354fcf65 -[UILabel setText:] + 32
6 Curl 0x0000682b -[RoutineTableViewController tableView:cellForRowAtIndexPath:] + 286
7 UIKit 0x355589ed -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 516
8 UIKit 0x3555876b -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 34
9 UIKit 0x3562a053 -[_UITableViewUpdateSupport(Private) _setupAnimationsForNewlyInsertedCells] + 3722
10 UIKit 0x35627b99 -[_UITableViewUpdateSupport initWithTableView:updateItems:oldRowData:newRowData:oldRowRange:newRowRange:context:] + 320
11 UIKit 0x35626dc1 -[UITableView(_UITableViewPrivate) _updateWithItems:withOldRowData:oldRowRange:newRowRange:context:] + 972
12 UIKit 0x35626473 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 4750
13 UIKit 0x3562501d -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] + 204
14 UIKit 0x356302b9 -[UITableView insertRowsAtIndexPaths:withRowAnimation:] + 20
15 Curl 0x00005dc3 -[RoutineTableViewController addEvent] + 398
16 Curl 0x000064b1 -[RoutineTableViewController alertView:willDismissWithButtonIndex:] + 228
17 UIKit 0x35621ee1 -[UIAlertView dismissWithClickedButtonIndex:animated:] + 192
18 UIKit 0x35621d25 -[UIAlertView(Private) _buttonClicked:] + 280
19 CoreFoundation 0x322b5571 -[NSObject(NSObject) performSelector:withObject:withObject:] + 24
20 UIKit 0x35513ec9 -[UIApplication sendAction:to:from:forEvent:] + 84
21 UIKit 0x35513e69 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 32
22 UIKit 0x35513e3b -[UIControl sendAction:to:forEvent:] + 38
23 UIKit 0x35513b8d -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 356
24 UIKit 0x35514423 -[UIControl touchesEnded:withEvent:] + 342
25 UIKit 0x35512bf5 -[UIWindow _sendTouchesForEvent:] + 368
26 UIKit 0x3551256f -[UIWindow sendEvent:] + 262
27 UIKit 0x354fb313 -[UIApplication sendEvent:] + 298
28 UIKit 0x354fac53 _UIApplicationHandleEvent + 5090
29 GraphicsServices 0x30557e77 PurpleEventCallback + 666
30 CoreFoundation 0x3231ca97 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
31 CoreFoundation 0x3231e83f __CFRunLoopDoSource1 + 166
32 CoreFoundation 0x3231f60d __CFRunLoopRun + 520
33 CoreFoundation 开发者_运维知识库 0x322afec3 CFRunLoopRunSpecific + 230
34 CoreFoundation 0x322afdcb CFRunLoopRunInMode + 58
35 GraphicsServices 0x3055741f GSEventRunModal + 114
36 GraphicsServices 0x305574cb GSEventRun + 62
37 UIKit 0x35525d69 -[UIApplication _run] + 404
38 UIKit 0x35523807 UIApplicationMain + 670
39 Curl 0x0000304f main + 82
40 Curl 0x00002ff8 start + 40
)
terminate called after throwing an instance of 'NSException'
(gdb)
Here is the issue in the above code
cell.textLabel.text = [self.eventsArray objectAtIndex:indexPath.row];
because
[eventsArray insertObject:routine atIndex:0];
eventsArray
hold the object of Routine
class not NSString
.
So Do'nt do
cell.textLabel.text = [self.eventsArray objectAtIndex:indexPath.row];
Edited:
Routine* myRoutine = [self.eventsArray objectAtIndex:indexPath.row];
//you need to get your stored text value from Routine class
//let's say it myText
cell.textLabel.text = myText;
I think the error clearly states -[Routine isEqualToString:]
: unrecognized selector is the problem. Pls check this.
Use the debbuger to see [RoutineTableViewController tableView:cellForRowAtIndexPath:] method when you app crashes. If you want to do:
cell.textLabel.text = [self.eventsArray objectAtIndex:indexPath.row];
You have to make sure you are putting NSString objects in eventsArray and not Routine objects.
精彩评论