gpt4 book ai didi

ios - 删除按钮没有出现在 UITableViewCell 中

转载 作者:行者123 更新时间:2023-11-28 21:56:41 25 4
gpt4 key购买 nike

我在单击“编辑”时获取“删除”按钮时遇到问题。问题是它根本没有出现。我的代码在下面,它适用于在单元格中滑动...提前感谢您的帮助!

#import "ViewController.h"

@interface ViewController ()

@property (strong) NSArray *lessons;

@end

static NSString *identifier = @"Cell";

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.lessons = @[
@"Computer Science",
@"Math",
@"Chemistry"
];

self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];

self.navigationItem.rightBarButtonItem = self.editButtonItem;

[self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:identifier];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.lessons.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

cell.textLabel.text = self.lessons[indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;
}

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

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}

@end

一旦我按下编辑按钮。

当我滑过单元格时出现。 enter image description here

最佳答案

听到上面的编码,看到它真的很有用。

  - (HelloController *) init
{
if (!(self = [super init])) return self;

self.title = @"Table Edits";

// Initialize the table titles, so they can be edited over time
tableTitles = [[NSMutableArray alloc] init];
ithTitle = NCELLS;
for (int i = 1; i <= NCELLS; i++)
[tableTitles addObject:[NSString stringWithFormat:@"Table Cell #%d", i]];

return self;
}

#pragma mark UITableViewDataSource Methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"any-cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"any-cell"] autorelease];
}

cell.text = [tableTitles objectAtIndex:[indexPath row]];
// cell.editingStyle = UITableViewCellEditingStyleDelete; // now read-only and no longer needed
return cell;
}

- (void) add
{
[tableTitles addObject:[NSString stringWithFormat:@"Table Cell #%d", ++ithTitle]];
[self.tableView reloadData];
}

#pragma mark UITableViewDelegateMethods
- (void) deselect
{
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

// Respond to user selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath
{
printf("User selected row %d\n", [newIndexPath row] + 1);
[self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];
}

-(void)leaveEditMode
{
// Add the edit button
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"Edit"
style:UIBarButtonItemStylePlain
target:self
action:@selector(enterEditMode)] autorelease];
[self.tableView endUpdates];
[self.tableView setEditing:NO animated:YES];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
printf("About to delete item %d\n", [indexPath row]);
[tableTitles removeObjectAtIndex:[indexPath row]];
[tableView reloadData];
}

-(void)enterEditMode
{
// Add the edit button
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"Done"
style:UIBarButtonItemStylePlain
target:self
action:@selector(leaveEditMode)] autorelease];
[self.tableView setEditing:YES animated:YES];
// [self.tableView beginUpdates];
}

- (void)loadView
{
[super loadView];

// Add an add button
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"New"
style:UIBarButtonItemStylePlain
target:self
action:@selector(add)] autorelease];

// Add the edit button
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"Edit"
style:UIBarButtonItemStylePlain
target:self
action:@selector(enterEditMode)] autorelease];
}

关于ios - 删除按钮没有出现在 UITableViewCell 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26314768/

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