gpt4 book ai didi

ios - Xcode 滑动和删除

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:51:05 25 4
gpt4 key购买 nike

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

我的.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com