2014-10-11 111 views
0

我得到一个问题,获取删除按钮编辑一次点击。问题在于它根本没有出现。我的代码在下面,它适用于在单元格中滑动...提前感谢您的帮助!删除按钮不会出现在UITableViewCell

#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

+0

如何创建编辑按钮? – pbasdf 2014-10-11 12:46:32

+0

感谢您的评论,我已经解决了这个问题! – Majotron 2014-10-11 13:26:30

回答

1

如果任何人有同样的问题,那么你可能在UIViewController使用UITableView,所以它不会为你管理版本。当您单击编辑按钮时,您必须手动实现TableView的编辑模式和视图的编辑模式。我怎么解决这个问题。

#pragma mark - Edit button listener 

- (void)editButtonPressed { 
    if(self.editing) { 
     [self setEditing:NO animated:YES]; 
     [self.tableView setEditing:NO animated:YES]; 
    } else { 
     [self setEditing:YES animated:YES]; 
     [self.tableView setEditing:YES animated:YES]; 
    } 
} 

谢谢你们的评论!

1

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

- (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]; 
    } 
相关问题