2011-04-08 101 views
0

我是一名新的iPhone應用程序開發人員。當我按下iPhone中的模擬器中的刪除按鈕時,我的應用程序不起作用。我在這裏從我的項目中複製了一些代碼。如果可能的話,我希望我能解決這個錯誤。我已經厭倦了發現這個問題,希望能幫助我解決。iPhone:刪除按鈕不起作用

我的代碼在這裏。

FirstPage.h

#import <UIKit/UIKit.h> 


@interface FirstPage : UIViewController <UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>{ 
    IBOutlet UIDatePicker *datePicker; 
    IBOutlet UITextField *eventText; 
    IBOutlet UITableView *tableview; 
    NSMutableArray *Array; 
    NSArray *notificationArray; 

    UILocalNotification *notif; 
} 
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker; 
@property (nonatomic, retain) IBOutlet UITextField *eventText; 
@property (nonatomic, retain) IBOutlet UITableView *tableview; 
- (IBAction) scheduleAlarm:(id) sender; 
//add,delete.edit table 

-(IBAction)EditTable:(id)sender; 


@end 

ñ我在這裏FirstPage.m。

#import "FirstPage.h" 


@implementation FirstPage 
@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 ) 
                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; 
} 
*/ 
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { 
    [eventText resignFirstResponder]; 
    return YES; 
} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
//Array = [[NSMutableArray alloc]init]; 
    //notificationArray = [[NSArray alloc] initWithArray:Array]; 
    Array = [notificationArray mutableCopy]; 
    eventText.delegate = self; 
    [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. 
    int count = [Array count]; 
    if(self.editing) { 
     count++; 
     //return [[UIApplication sharedApplication]scheduleLocalNotifications]; 
     return count; 
    }else { 
     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]; 
// notificationArray = Array; 
// [notificationArray mutableCopy]; 
    notif = [notificationArray objectAtIndex:indexPath.row]; 

    [cell.textLabel setText:notif.alertBody]; 
    [cell.detailTextLabel setText:[notif.fireDate description]]; 

    return cell; 
} 

- (IBAction)DeleteButtonAction:(id)sender 
{ 
// [Array removeLastObject]; 
    [Array removeLastObject]; 
    NSLog(@"testing in delete button..."); 
    //[tableview reloadData]; 
} 

- (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]; 
    } 
} 

// The editing style for a row is the kind of button displayed to the left of the cell when in editing mode. 
- (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.  

     return UITableViewCellEditingStyleDelete; 

} 

// Update the data model according to edit actions delete or insert. 
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
    // [Array removeObjectAtIndex:[indexPath indexAtPosition:1]]; 
     [Array removeObjectAtIndex:indexPath.row]; 
    // [tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; 

     NSLog(@"at delete button mah?..."); 
     [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 = [[Array objectAtIndex:fromIndexPath.row] retain]; 
    [Array removeObject:item]; 
    [Array insertObject:item atIndex:toIndexPath.row]; 
    [item release]; 
} 


/* 
// 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 { 
    datePicker = nil; 
    tableview = nil; 
    eventText = nil; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


@end 
+0

請解釋'你不工作'的含義。如果不知道問題是什麼,很難提供幫助 - 任何相關的錯誤消息都會有所幫助。 – 2011-04-08 10:52:28

+0

在我的表格視圖中我有一些數據row.then當我點擊刪除按鈕將無法正常工作。 – ahyong87 2011-04-08 11:02:17

+0

由'not function',你的意思是刪除按鈕不做任何事情,或者整個應用程序崩潰? – 2011-04-08 11:03:22

回答

0

你很難從對這個問題提出的事實的回答,但它看起來像你對我有沒有在你的代碼的刪除按鈕創建一個IBOutlet中,你沒有把它設置及其在IB代理。

+0

感謝您的回覆,我會再次檢查。 – ahyong87 2011-04-11 02:26:53