在我的应用程序中,当我按下删除行将崩溃。我试图找到解决这个问题的方法,希望可以帮我解决。
我的代码在这里:
FirstView.h
#import <UIKit/UIKit.h>
@interface FirstView : UIViewController <UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>{
IBOutlet UITableView *tableview;
IBOutlet UIDatePicker *datePicker;
IBOutlet UITextField *eventText;
NSArray *notificationArray;
NSMutableArray *Array;
}
@property (nonatomic, retain) IBOutlet UITableView *tableview;
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
@property (nonatomic, retain) IBOutlet UITextField *eventText;
- (IBAction) scheduleAlarm:(id) sender;
@end
FirstView.m
#import "FirstView.h"
@implementation FirstView
@synthesize datePicker, eventText,tableview;
- (void) viewWillAppear:(BOOL)animated {
[self.tableview reloadData];
}
- (IBAction) scheduleAlarm:(id) sender {
[eventText resignFirstResponder];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = [eventText text];
// Set the action button
localNotif.alertAction = @"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
[self.tableview reloadData];
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
eventText.delegate = self;
Array = [notificationArray mutableCopy];
[super viewDidLoad];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
}
// 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:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];
[cell.textLabel setText:notif.alertBody];
[cell.detailTextLabel setText:[notif.fireDate description]];
return cell;
}
- (void)tableView:(UITableView *)tv commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete) {
[Array removeObjectAtIndex:indexPath.row];
[self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[eventText resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
datePicker = nil;
eventText = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
此处的崩溃日志。
2011-04-12 13:27:26.860 SMS Timer[2050:207] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:995
2011-04-12 13:27:26.863 SMS Timer[2050:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dca5a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f1e313 objc_exception_throw + 44
2 CoreFoundation 0x00d82ef8 +[NSException raise:format:arguments:] + 136
3 Foundation 0x000b53bb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x00338e8b -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8420
5 UIKit 0x00327cf8 -[UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 56
6 SMS Timer 0x00003414 -[FirstView tableView:commitEditingStyle:forRowAtIndexPath:] + 189
7 UIKit 0x00325037 -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101
8 UIKit 0x002ba4fd -[UIApplication sendAction:to:from:forEvent:] + 119
9 UIKit 0x0034a799 -[UIControl sendAction:to:forEvent:] + 67
10 UIKit 0x0034cc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
11 UIKit 0x0034b7d8 -[UIControl touchesEnded:withEvent:] + 458
12 UIKit 0x002deded -[UIWindow _sendTouchesForEvent:] + 567
13 UIKit 0x002bfc37 -[UIApplication sendEvent:] + 447
14 UIKit 0x002c4f2e _UIApplicationHandleEvent + 7576
15 GraphicsServices 0x01722992 PurpleEventCallback + 1550
16 CoreFoundation 0x00dab944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
17 CoreFoundation 0x00d0bcf7 __CFRunLoopDoSource1 + 215
18 CoreFoundation 0x00d08f83 __CFRunLoopRun + 979
19 CoreFoundation 0x00d08840 CFRunLoopRunSpecific + 208
20 CoreFoundation 0x00d08761 CFRunLoopRunInMode + 97
21 GraphicsServices 0x017211c4 GSEventRunModal + 217
22 GraphicsServices 0x01721289 GSEventRun + 115
23 UIKit 0x002c8c93 UIApplicationMain + 1160
24 SMS Timer 0x00002754 main + 102
25 SMS Timer 0x000026e5 start + 53
)
terminate called after throwing an instance of 'NSException'
答案 0 :(得分:1)
- (void)tableView:(UITableView *)tv commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete) {
[Array removeObjectAtIndex:indexPath.row];
[self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//TRy to set your count for this line
//[[[UIApplication sharedApplication] scheduledLocalNotifications] count];IF you are getting this proper after deleting the cell, then it won;t crash.Since you are deleting the cell but not updating the count so still it return 5 intend of 4 after deleting the data.
[self.tableView reloadData];
}
希望有效......让我们亲自动手
答案 1 :(得分:0)
从崩溃日志中可以清楚地看到“第0节中的行数无效”是导致崩溃的原因。每次编辑单元格时,请调试应用程序以查看各部分的行。然后它将帮助您确定原因。