2

这是我的任务记录器代码。它工作正常,我只想在我的单元格中使用滑动和删除功能。就像您可以在音乐或电子邮件应用程序中执行的操作一样。如何编辑我当前的代码,以便可以滑动添加的每个单元格,然后弹出删除按钮。我已经看过代码示例,但我不明白如何将它集成到我的代码中。

我的.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];
    }
}
4

1 回答 1

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

于 2012-06-05T19:11:30.880 回答