Uitableview 如何在基于tabbar的应用程序中编辑表行

Uitableview 如何在基于tabbar的应用程序中编辑表行,uitableview,uitabbarcontroller,xcode4.2,Uitableview,Uitabbarcontroller,Xcode4.2,全部 我正在开发一个演示应用程序,使用包含3个选项卡的选项卡栏控制器。 在第一个选项卡中,我使用一个表视图控制器 我在编辑添加/删除表行时遇到问题。我无法从表中添加或删除行 当我在没有tabbar控件的情况下使用这个表视图时,我能够修改表。 在这里,我共享我的tableviewcontroller.h和.m文件 **tableviewcontroller.h** #import <UIKit/UIKit.h> @interface T

全部

我正在开发一个演示应用程序,使用包含3个选项卡的选项卡栏控制器。 在第一个选项卡中,我使用一个表视图控制器

我在编辑添加/删除表行时遇到问题。我无法从表中添加或删除行

当我在没有tabbar控件的情况下使用这个表视图时,我能够修改表。 在这里,我共享我的tableviewcontroller.h和.m文件

        **tableviewcontroller.h**
        #import <UIKit/UIKit.h>

        @interface TableViewViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>    
        {
               IBOutlet UITableView *tableView1;
               SMutableArray *arry;
               NSString *enteredText;
               UITextField *myTextField;
               NSInteger rowIndex;
            }
        @property(nonatomic,readonly)NSString *enteredText;
        - (IBAction) EditTable:(id)sender;
            @end

            **TableViewViewController.m**
            #import "TableViewViewController.h"
            @implementation TableViewViewController
            @synthesize enteredText;

        - (void)viewDidLoad 
        {
          arry=[[NSMutableArray alloc]initWithObjects:@"iPhone",@"MacMini",@"iMac",@"MacBookProAir",@"MacBookPro",nil];
          self.title = @"Table View ";
          UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(EditTable:)];
          [self.navigationItem setLeftBarButtonItem:addButton];
          [super viewDidLoad];
        }
        - (void)didReceiveMemoryWarning 
        {
          [super didReceiveMemoryWarning]; 
        }
- (void)dealloc 
{
  [super dealloc];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
  return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
 int count = [arry count];
 if(self.editing) count++;
 return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil)
  {
    cell = [[[UITableViewCell alloc]initWithFrame:CGRectZero    reuseIdentifier:CellIdentifier]autorelease];
  }
  int count = 0;
  if(self.editing && indexPath.row != 0)
    count = 1;
  if(indexPath.row == ([arry count]) && self.editing)
  {
    cell.textLabel.text = @"ADD";
        return cell;
  }
  cell.textLabel.text = [arry objectAtIndex:indexPath.row];
  return cell;
}
- (IBAction)AddButtonAction:(id)sender
{
   [arry addObject:@"asd"];
   [tableView1 reloadData];
}
- (IBAction)DeleteButtonAction:(id)sender
{
   [arry removeLastObject];
   [tableView1 reloadData];
}
- (IBAction) EditTable:(id)sender
{
   if(self.editing)
   {
      [super setEditing:NO animated:NO]; 
      [tableView1 setEditing:NO animated:NO];
      [tableView1 reloadData];
      [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
      [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
   }
   else
   {
      [super setEditing:YES animated:YES]; 
      [tableView1 setEditing:YES animated:YES];
      [tableView1 reloadData];
      [self.navigationItem.leftBarButtonItem setTitle:@"Done"];
      [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
   }
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   if (self.editing == NO || !indexPath) 
        return UITableViewCellEditingStyleNone;
   if (self.editing && indexPath.row == ([arry count])) 
   {
        return UITableViewCellEditingStyleInsert;
   } 
   else 
   {
    return UITableViewCellEditingStyleDelete;
   }
        return UITableViewCellEditingStyleNone;
}
-(void)tableView:(UITableView *)aTableView commitEditingStyle:UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
   if (editingStyle == UITableViewCellEditingStyleDelete) 
   {
      //[arry removeObjectAtIndex:indexPath.row];
      rowIndex=indexPath.row;
      UIActionSheet *actionSheet=[[UIActionSheet alloc]
      initWithTitle:@"Are you sure you want to delete this ?"
      delegate:self
      cancelButtonTitle:@"Cancel"
      destructiveButtonTitle:@"Delete"
      otherButtonTitles:nil,nil];
      [actionSheet showInView:self.view];
      [actionSheet  release];
   } 
   else if(editingStyle == UITableViewCellEditingStyleInsert)
   {
      UIAlertView *alert = [[UIAlertView alloc] 
                       initWithTitle:@"Enter Row value" 
                       message:@"blank" 
                       delegate:self 
                       cancelButtonTitle:@"Dismiss"
                       otherButtonTitles:@"OK!", nil];
      myTextField= [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
      [myTextField setBackgroundColor:[UIColor whiteColor]];
      [alert addSubview:myTextField];
      [alert show];
      [alert release];
   }
}

-(void) actionSheet:(UIActionSheet *) actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex;
{
   if(buttonIndex==0)
   {
      [arry removeObjectAtIndex:rowIndex ];
      [tableView1 reloadData];
   }
   else if (buttonIndex==1)
   {
      [tableView1 reloadData];
   }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if (buttonIndex == 0)
   {
      [tableView1 reloadData];
   }
   else if (buttonIndex == 1 && myTextField.hasText!=NO)
   {   
      NSString *txt=[myTextField text];
     [arry addObject:txt ];
     [tableView1 reloadData];
   }
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
   return YES;
}
@end

出于好奇,为什么要使用TableVIewController:UIViewController,而不仅仅是让TableVIewController从UITableViewController继承?是否在此视图中显示任何其他内容?因为如果不是,您可能应该将视图控制器子类化为最接近您想要的视图控制器,即UITableViewController


如果这样做,还可以通过tableView或self.tableView访问此视图的默认表,但这不是必需的属性。这不是问题的原因,但这是一种很好的做法。

出于好奇,为什么要使用TableVIewController:UIViewController,而不仅仅是让TableVIewController从UITableViewController继承?是否在此视图中显示任何其他内容?因为如果不是,您可能应该将视图控制器子类化为最接近您想要的视图控制器,即UITableViewController


如果这样做,还可以通过tableView或self.tableView访问此视图的默认表,但这不是必需的属性。这不是问题的原因,但这是一种很好的做法。

尝试self.tableView1.editing而不是self.editing并设置tableView1的委托和数据源项目try self.tableView1.editing而不是self.editing并设置tableView1的委托和数据源项目