2014-10-11 155 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]; 
    } 
相關問題