1

7 セクションのテーブルがあり、各セクションに 2 行ありますが、削除ボタンが機能しない理由がわかりません!! セルに表示されない

助けてください!前もって感謝します!

ここに画像の説明を入力

日.m

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    arry = [[NSMutableArray alloc] init];
    [arry addObject:@"test"];
    [arry addObject:@"hey"];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

-(void)addNewItem
{
    [arry addObject:@"New Day"];
    [self.tableView reloadData];
}

// ... standard methods to override

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 7;
    //_week.days.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [arry count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.text = [arry objectAtIndex:indexPath.row];
    return cell;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return @"Monday";
    }
    else if(section == 1)
    {
        return @"Tuesday";
    }
    else if(section == 2)
    {
        return @"Wednesday";
    }
    else if(section == 3)
    {
        return @"Thuesday";
    }
    else if(section == 4)
    {
        return @"Friday";
    }
    else if(section == 5)
    {
        return @"Saturday";
    }
    else
    {
        return @"Sunday";
    }
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // [self.monthTitle removeObjectAtIndex:indexPath.row];
        [arry removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];
    }
    else if (editingStyle==UITableViewCellEditingStyleInsert)
    {   
    }   
}

Edit1:ストーリーボードから複数の選択を削除した後、削除ボタンが表示されますが、削除ボタンをクリックすると終了します

ここに画像の説明を入力

4

3 に答える 3

1

あなたのスクリーンショットから、あなたが設定allowsMultipleSelectionDuringEditingしたように見えますYES(コードでそれを見ることができないので、おそらくあなたのnibファイルで)。その場合、tableView:commitEditingStyle:forRowAtIndexPath:チェックマークはユーザーの選択を示すだけであり、これらの行を削除する必要はないため、呼び出されることはありません。

編集モードに入ったときにアクティブになる独自の削除ボタンが必要です。これは、メール アプリで表示されるのと同じです。

于 2012-07-27T11:34:59.987 に答える
1

テーブルの更新を開始していません。メソッド「deleteRowsAtIndexPath」を使用する場合は、次のメソッドの間に挿入する必要があります。

// Begin update
[tableView beginUpdates];

// Perform update
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                 withRowAnimation:UITableViewRowAnimationFade];

// End update
[tableView endUpdates]
于 2012-07-27T10:44:12.433 に答える
0

最初に、次のようにデータソースから行を削除する必要があります。

            [self.table_favpropertylist_array removeObjectAtIndex:indexPathRow];
            [tbl_FavDetails beginUpdates];

            UITableViewCell *cell=[tbl_FavDetails cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPathRow inSection:0]];

            NSIndexPath *indexPath = [tbl_FavDetails indexPathForCell:cell];
            [tbl_FavDetails deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                                  withRowAnimation:UITableViewRowAnimationFade];
            [tbl_FavDetails endUpdates];

ここで、self.table_favpropertylist_array はデータソース、tbl_FavDetails は tableview、indexPathRow は行削除ボタンがクリックされたインデックスです。

ありがとう。

于 2013-07-31T08:17:41.560 に答える