2

好的,所以我的项目(确实有 Cocos2d)有 3 个 UITableView。到目前为止,我只关心第一个,这就是为什么我只粘贴第一个的代码。问题是,每当我使用该应用程序时,都会显示表格视图,其中包含所有内容。编辑/完成按钮也显示在右上角。但是,每当我尝试点击编辑按钮时,按钮会切换到完成,但单元格附近没有删除图标,我也无法删除单元格。我尝试了很多解决方案,但似乎没有一个有效。提前致谢。

-(id)init{
if (self == [super init]) {
    // Create initialized instance of UITabBarController
    tabController = [[UITabBarController alloc] init];
    // Create initialized instance of NSMutableArray to hold our UINavigationControllers
    tabArray = [[NSMutableArray alloc] init];

    // Create first UIViewController
    questionController = [[UIViewController alloc] init];
    [self makeQuestionTable];
    [questionController setTitle:@"Question"];
    // Initialize the UINavigationController
    navigationController = [[UINavigationController alloc] initWithRootViewController:questionController];
    questionController.navigationItem.rightBarButtonItem = questionController.editButtonItem;
    questionTableView.allowsSelection = YES;
    questionTableView.allowsMultipleSelectionDuringEditing = YES;
    questionTableView.allowsSelectionDuringEditing = YES;
    // Add UINavigationController to you tabs
    [tabArray addObject:navigationController];
    // Release UIViewController and UINavigationController
    [questionController release], [navigationController release];

    // Create second UIViewController
    answerController = [[UIViewController alloc] init];
    [self makeAnswerTable];
    [answerController setTitle:@"Answer"];
    // Initialize the UINavigationController
   navigationController = [[UINavigationController alloc] initWithRootViewController:answerController];
    // Add UINavigationController to you tabs
    [tabArray addObject:navigationController];
    // Release UIViewController and UINavigationController
    [answerController release], [navigationController release];


    // Create third UIViewController
    colorController = [[UIViewController alloc] init];
    [self makeColorTable];
    [colorController setTitle:@"Color"];
    // Initialize the UINavigationController
    navigationController = [[UINavigationController alloc] initWithRootViewController:colorController];
    // Add UINavigationController to you tabs
    [tabArray addObject:navigationController];
    // Release UIViewController and UINavigationController
    [colorController release], [navigationController release];

    // Add the tabs to the UITabBarController
    [tabController setViewControllers:tabArray];
    // Add the view of the UITabBarController to the window
    [[[CCDirector sharedDirector]view]addSubview:tabController.view];
}
return self;
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{

[questionTableView setEditing:editing animated:animated];
[questionController setEditing:editing animated:animated];

}
-(void)makeQuestionTable{
questionTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)          style:UITableViewStylePlain];
questionTableView.backgroundColor = [UIColor whiteColor];
questionTableView.delegate = self;
questionTableView.dataSource = self;
questionTableView.tag = 1;
[questionTableView reloadData];
[questionController.view addSubview:questionTableView];
}

-(void)makeAnswerTable{
answerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)     style:UITableViewStylePlain];
answerTableView.backgroundColor = [UIColor whiteColor];
answerTableView.delegate = self;
answerTableView.dataSource = self;
answerTableView.tag = 2;
[answerTableView reloadData];
[answerController.view addSubview:answerTableView];

}

-(void)makeColorTable{

colorTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 400) style:UITableViewStylePlain];
colorTableView.backgroundColor = [UIColor whiteColor];
colorTableView.delegate = self;
colorTableView.dataSource = self;
colorTableView.tag = 3;
[colorTableView reloadData];
[colorController.view addSubview:colorTableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  if (currentTab == 1) {
    return [appDelegate.questionArray count];
}
else if (currentTab == 2) {
    return [appDelegate.answerArray count];
}
else if (currentTab == 3) {
    return [appDelegate.colorArray count];
}
else{
    return 0;
}
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
if (tableView.tag == 1) {


static NSString *simpleTableIdentifier = @"SimpleTableItem";

UITableViewCell *cell = [tableView     dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [appDelegate.questionArray objectAtIndex:indexPath.row];
    return cell;
    NSLog(@"Question");
}
if(tableView.tag == 2){

    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.textLabel.text = [appDelegate.answerArray objectAtIndex:indexPath.row];
    return cell;
    NSLog(@"Answer");
}
if(tableView.tag == 3){

    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.textLabel.text = [appDelegate.colorArray objectAtIndex:indexPath.row];
    return cell;
    NSLog(@"Color");
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView
       editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath {
UITableViewCell *cell = [questionTableView cellForRowAtIndexPath:indexPath];
if ([cell isEditing]) {
    NSLog(@"YES");
}
else{
    NSLog(@"NO");
}
return UITableViewCellEditingStyleDelete;
}
// This only needs to be implemented if you are going to be returning NO
// for some items. By default, all items are editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath     {
// Return YES if you want the specified item to be editable.
return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
    //add code here for when you hit delete
    [questionTableView delete:indexPath];
}
}
4

3 回答 3

0

将 tableView 的属性“编辑”设置为“是”。像:

questionTableView.editing = YES;

删除这一行:

questionTableView.allowsMultipleSelectionDuringEditing = YES;

allowsMultipleSelectionDuringEditing选择时设置为“否”。

于 2013-01-10T09:18:26.273 回答
0

在您的edit按钮操作中,您需要提供setEditingas YES

[tableView setEditing:YES animated:YES];
于 2013-01-10T09:29:03.820 回答
0

你之前错过了几个右花括号- (void)setEditing,你的 init 方法不应该return self;

于 2013-01-11T03:29:25.593 回答