iPhone中报警应用程序的删除功能出错
每当我在iPhone的模拟器上按下删除按钮时,我的应用程序就会崩溃。我从我的项目中复制了一些代码。如何解决删除错误 EnableAlarm.h文件iPhone中报警应用程序的删除功能出错,iphone,objective-c,Iphone,Objective C,每当我在iPhone的模拟器上按下删除按钮时,我的应用程序就会崩溃。我从我的项目中复制了一些代码。如何解决删除错误 EnableAlarm.h文件 #import <UIKit/UIKit.h> @interface EnableAlarm : UIViewController <UITableViewDataSource,UITableViewDelegate>{ IBOutlet UITableView *tableview; IBOutlet U
#import <UIKit/UIKit.h>
@interface EnableAlarm : UIViewController <UITableViewDataSource,UITableViewDelegate>{
IBOutlet UITableView *tableview;
IBOutlet UIDatePicker *datePicker;
IBOutlet UITextField *eventText;
NSMutableArray *arraydata;
}
-(IBAction) btnReturn:(id) sender;
@property (nonatomic, retain) IBOutlet UITableView *tableview;
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
@property (nonatomic, retain) IBOutlet UITextField *eventText;
- (IBAction) scheduleAlarm:(id) sender;
- (IBAction) EditTable:(id)sender;
@end
我发现了一些问题:
- 在
if语句无效,因为if-和else分支返回相同的值tableView:editingstyleforrowatinexpath:
- 除非您自己添加了视图(即使用
),否则不要调用addSubview
)removeFromSuperview
- 您不需要在
视图中调用
,它将显示[tableview reloadData]
- 在
中,本地int变量tableView:numberofrowsin节:
无效count
- 在
中删除行“tableView:committeeditingstyle:
[tableView removeFromSuperview:indexPath.row]代码>'。这种方法不存在;这可能就是它崩溃的原因
- 从数组
中删除2项。这可能不是你想要的arraydata
- 在
tableView:committeeditingstyle:
中的清理丢失dealloc
- 建议:所有方法和变量都应以小写字母开头
#import "EnableAlarm.h"
@implementation EnableAlarm
-(IBAction) btnReturn:(id) sender
{
[self.view removeFromSuperview];
}
@synthesize datePicker,tableview, eventText;
- (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 localTimeZone];
// 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];
}
#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.
int count = [arraydata count];
if(self.editing) count++;
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];
}
int count = 0;
if(self.editing && indexPath.row != 0)
count = 1;
// Configure the cell...
NSArray *arraydata = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notif = [arraydata objectAtIndex:indexPath.row];
[cell.textLabel setText:notif.alertBody];
[cell.detailTextLabel setText:[notif.fireDate description]];
return cell;
}
- (IBAction) EditTable:(id)sender{
if(self.editing)
{
[super setEditing:NO animated:NO];
[tableview setEditing:NO animated:NO];
[tableview reloadData];
//[self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
//[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[tableview setEditing:YES animated:YES];
[tableview reloadData];
//[self.navigationItem.leftBarButtonItem setTitle:@"Done"];
//[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
// arraydata = [[NSMutableArray alloc] initWithObjects:nil];;
[super viewDidLoad];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// No editing style if not editing or the index path is nil.
//if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;
// Determine the editing style based on whether the cell is a placeholder for adding content or already
// existing content. Existing content can be deleted.
if (self.editing && indexPath.row == ([arraydata count])) {
return UITableViewCellEditingStyleDelete;
} else {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
// Update the data model according to edit actions delete or insert.
- (void)tableView:(UITableView *)TableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[arraydata removeObjectAtIndex:indexPath.row];
NSString *item = [[arraydata objectAtIndex:indexPath.row] retain];
[arraydata removeObject:item];
[TableView removeFromSuperview:indexPath.row];
[item release];
//if (editingStyle == UITableViewCellEditingStyleDelete) {
// [arraydata removeObjectAtIndex:indexPath.row];
// NSMutableArray * mArray = [NSMutableArray arrayWithContentsOfURL:theNewURL];
// [mArray removeObjectAtIndex:indexPath.row];
[tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];
[tableview reloadData];
}
}
#pragma mark Row reordering
// Determine whether a given row is eligible for reordering or not.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// Process the row move. This means updating the data model to correct the item indices.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [[arraydata objectAtIndex:fromIndexPath.row] retain];
[arraydata removeObject:item];
[arraydata insertObject:item atIndex:toIndexPath.row];
[item release];
}
// 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;
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (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];
datePicker = nil;
tableview = nil;
eventText = nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end