5

iOS 6 でアプリが正常に動作していました。アップグレードにより、 から行を削除できなくなりましたUITableView

プロトタイプセルにボタンがあります。

ボタンの機能は次のとおりです。

- (IBAction)deleteCell:(id)sender {
    UIButton *btn = (UIButton*) sender;
    UITableViewCell *cell = (UITableViewCell*) btn.superview.superview;
    NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
    if([names count] > 9) {
        int index = indexPath.row;
        [names removeObjectAtIndex:index];
        [photos removeObjectAtIndex:index];
        [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    } 
} 

問題は、インデックス変数が常に 0 であることです。

別の解決策を試しました:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

        if (editingStyle == UITableViewCellEditingStyleDelete) {
            int index = indexPath.row;
            [names removeObjectAtIndex:index];
            [photos removeObjectAtIndex:index];
            [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];

}
}

この解決策も機能しません。右にスワイプしても何も起こりません。

4

6 に答える 6

6

このサンプル チュートリアルを試してください。

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
  NSMutableArray *arryData1;
  NSMutableArray *arryData2;
  IBOutlet UITableView *tableList;
}

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

 @end

@implementation ViewController

-(void)viewDidLoad
{
[super viewDidLoad];

arryData1 = [[NSMutableArray alloc] initWithObjects:@"MCA",@"MBA",@"BTech",@"MTech",nil];
arryData2 = [[NSMutableArray alloc] initWithObjects:@"Objective C",@"C++",@"C#",@".net",nil];
tableList.editing=YES;

}

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arryData1 count];
}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text = [arryData1 objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [arryData2 objectAtIndex:indexPath.row];
return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {
    int index = indexPath.row;
    [arryData1 removeObjectAtIndex:index];
    [arryData2 removeObjectAtIndex:index];

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationFade];


}
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableList.editing)
{
    return UITableViewCellEditingStyleDelete;
}

return UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
于 2013-09-23T15:09:08.053 に答える
2

問題は、データを再度リロードしないため、キャッシュメモリに残っていることです

[tableView reloadData];このメソッドを 使用し[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; て、2 番目のソリューションのこのコード行の下にデータを再読み込みできます。

于 2014-05-02T07:52:14.520 に答える
1

tableview commiteditingstyle メソッドを使用して、tableviewcontroller のセル/行を削除します。

実行可能なコードスニップは次のとおりです。

-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    [ArrayHoldsCellObj removeObjectAtIndex:indexPath.row];

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

ArrayHoldsCellObjは、宣言して各セルをエリア インデックスに割り当てる必要がある Array オブジェクトであることに注意してください。

于 2014-05-05T23:50:30.023 に答える
0

でボタンを設定できtag = cell.index ますcellForRowAtIndexPath

その後

UITableViewCell *cell = (UITableViewCell *)
        [tableView cellForRowAtIndexPath:
        [NSIndexPath indexPathForRow:btn.tag inSection:0]];

インデックスを取得できるように

于 2013-09-23T14:28:25.007 に答える