2012-06-05 83 views
2

這是我的任務記錄器代碼。它工作正常,我只想但我的單元格中的幻燈片和刪除功能。就像您在音樂或電子郵件應用程序中可以做的一樣。如何編輯我的當前代碼,以便每個添加的單元格都可以滑過,然後彈出刪除按鈕。我看過代碼的例子,但我不明白如何將它集成到我的代碼中。Xcode幻燈片並刪除

我的.h:

#import <UIKit/UIKit.h> 

NSString *docPath(void); 

@interface BNRAppDelegate : UIResponder 
<UIApplicationDelegate, UITableViewDataSource> 
{ 

    UITableView *taskTable; 
    UITextField *taskField; 
    UIButton *insertButton; 
    UIButton *clearButton; 

    NSMutableArray *tasks; 
} 

- (void)addTask:(id)sender; 
- (void)takeTask:(id)sender; 


@property (strong, nonatomic) UIWindow *window; 

@end 

這是我的.m

#import "BNRAppDelegate.h" 


NSString *docPath() 
{ 
    NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                  NSUserDomainMask, 
                  YES); 
    return [[pathList objectAtIndex:0] stringByAppendingPathComponent:@"data.td" ]; 
}  

@implementation BNRAppDelegate 

@synthesize window = _window; 

#pragma mark - Application delegate callbacks 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:     (NSDictionary *)launchOptions 
{ 
    NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()]; 
    if (plist) 
    { 
     tasks = [plist mutableCopy]; 
    } 
    else 
    { 
     tasks = [[NSMutableArray alloc] init]; 
    } 

    CGRect windowFrame = [[UIScreen mainScreen] bounds]; 
    UIWindow *theWindow = [[UIWindow alloc] initWithFrame:windowFrame]; 
    [self setWindow:theWindow]; 

    CGRect tableFrame = CGRectMake(0, 80, 320, 380); 
    CGRect fieldFrame = CGRectMake(5, 40, 180, 31); 
    CGRect buttonFrame = CGRectMake(190, 40, 60, 31); 
    CGRect clearFrame = CGRectMake(255, 40 , 60, 30); 


    taskTable = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain]; 
    [taskTable setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 
    [taskTable setDataSource:self]; 


    taskField = [[UITextField alloc] initWithFrame:fieldFrame]; 
    [taskField setBorderStyle:UITextBorderStyleRoundedRect]; 
    [taskField setPlaceholder:@"Type a task, tap Insert"]; 


    insertButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [insertButton setFrame:buttonFrame]; 
    [insertButton addTarget:self action:@selector(addTask:) forControlEvents:UIControlEventTouchUpInside]; 
    [insertButton addTarget:self action:@selector(addButton:) forControlEvents:UIControlEventTouchUpInside]; 
    [insertButton setTitle:@"Insert" forState:UIControlStateNormal]; 


    clearButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [clearButton setFrame:clearFrame]; 
    [clearButton addTarget:self action:@selector(takeTask:)  forControlEvents:UIControlEventTouchUpInside]; 
    [clearButton setTitle:@"Clear" forState:UIControlStateNormal]; 


    //For Clear make simular to ^^^ then add void. In Void use the variable t.    

    [[self window] addSubview:taskTable]; 
    [[self window] addSubview:taskField]; 
    [[self window] addSubview:insertButton]; 
    [[self window] addSubview:clearButton]; 

    [[self window] setBackgroundColor:[UIColor whiteColor]]; 
    [[self window] makeKeyAndVisible]; 

    return YES; 
} 
- (void)addTask:(id)sender 
{ 
    NSString *t=[taskField text]; 

    if ([t isEqualToString:@""]) { 
     return; 
    } 

    [tasks addObject:t]; 
    [taskTable reloadData]; 
    [taskField setText:@""]; 
    [taskField resignFirstResponder]; 
} 

- (void)takeTask:(id)sender 
{ 
    //LEARN ABOUT NSMUTABLEARRAYS AND HOW TO TAKE DADA FROM THEM 
    [tasks removeAllObjects]; 
    [taskTable reloadData]; 
    [tasks writeToFile:docPath() 
      atomically:YES]; 
} 


#pragma mark - Table View management 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [tasks count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 

{ 
    UITableViewCell *c= [taskTable dequeueReusableCellWithIdentifier:@"Cell"]; 

    if (!c) { 
     c= [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    } 

    NSString *item = [tasks objectAtIndex:[indexPath row]]; 
    [[c textLabel] setText:item]; 

    return c; 
} 


- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    [tasks writeToFile:docPath() 
      atomically:YES]; 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    [tasks writeToFile:docPath() 
      atomically:YES]; 
} 

@end 

這是我嘗試,但它不工作:

-(void)tableView:(UITableView *)table commitEditingStyle:  (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath *)indexPath 
{  
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
      [tasks removeObjectAtIndex:indexPath.row]; 
    } 
} 
+0

請學習如何使用谷歌和/或搜索 – Novarg

回答

1

在你的代碼,添加以下功能

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 

而且

- (void)tableView:(UITableView *)table commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     //Go ahead and delete the row data now 
    } 
} 

在功能commitEditingStyle,你現在必須刪除的實際單元格數據 例如,如果你有一個NSMutableArray包含單元格數據 現在,您可以從它 刪除實際的對象如下所示 [arr removeObjectAtIndex:indexPath.row];

+0

謝謝@OmarAbdelhafith。當我滑動刪除按鈕確實出現,但當我按下刪除按鈕什麼都沒有發生...... – user1438042

+0

請檢查編輯我做了答案:) –

+0

感謝您的幫助。我的NSMutableArray被稱爲「任務」。看看我的編輯,這是我的嘗試,但它不工作。感謝您的幫助 – user1438042