删除按钮不会出现在UITableViewCell中

时间:2014-10-11 12:36:52

标签: ios uitableview

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

#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

2 个答案:

答案 0 :(得分: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];
    }

答案 1 :(得分:1)

如果有人遇到同样的问题,那么您可能在UITableView中使用UIViewController,因此它不会为您管理版本。单击“编辑”按钮时,必须手动实现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];
    }
}

谢谢大家的评论!