ios - Xcode 滑动和删除

标签 ios iphone 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];
    }
}

最佳答案

在您的代码中,添加以下函数

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

关于ios - Xcode 滑动和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10903070/

相关文章:

ios - CLLocationManager 一直不工作(iOS 8,Xcode 6)

ios - 使用不同 ViewController 中的按钮更改 WKWebview URL

android - 如何获取 URL Scheme myapp ://to work with Instagram API redirect uri

ios - 测试对象是否在对象数组中

iphone - 自定义事件多久才会出现?

ios - 在 Swift 项目中包含基于方案的某些 Objective-C 文件

ios - 如何在 UITextField 中的文本上绘制边框

iphone - 为 iPad 构建 iPhone 应用程序

html - 如何为我的网站提供适用于 iPhone 的图标?

ios - 找不到 Plugin.h 文件 : flutter and xcode