2

私はiOSに非常に慣れていませんが、編集モードにしたいテーブルビューを持つView Controllerを作成しました。これは私のView Controller実装ファイルコードです:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize progsTable, programmes;

- (void)viewDidLoad
{
    [super viewDidLoad];
    programmes = [[NSMutableArray alloc] initWithObjects:@"one",@"two",@"three",nil];
    //set the title
    self.title = @"Programmes";

    //add an edit button
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [progsTable setEditing:editing animated:animated];
}

#pragma mark - UITableView Datasource

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

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = [programmes objectAtIndex: indexPath.row];

    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *) indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        //remove from array
        [programmes removeObjectAtIndex:indexPath.row];

        //remove from table
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
#pragma mark - UITableView Delegate methods

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

@end

左上に編集ボタンがありますが、クリックしても赤い削除ボタンが表示されません。何が間違っていますか?

4

4 に答える 4

2

以下のコードを追加する必要があると思います。

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

これにより、必要な編集スタイルが UITableView に伝えられます。

于 2013-10-28T15:46:28.183 に答える
1

これを使って

- (IBAction)deleteDrug:(id)sender event:(id)event {
    selectedButtonIndex = [self indexPathForButton:sender event:event];

    [tableView setEditing:YES animated:YES];
}


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

    return NO;
}

データソースにこのメソッドを実装する必要がある場合があります。

- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
于 2013-10-28T17:11:41.837 に答える