2012-04-06 4 views
2

에 테이블 행을 편집하는 방법 모든TabBar의 기반 응용 프로그램

내가 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 
+0

"self.editing"대신 "self.tableView1.editing"을 시도하고 tableView1의 대리인 및 데이터 소스 프로퍼티를 설정하십시오. – HarshIT

답변

0

, 왜 당신은 UITableViewController에서 TableViewController 상속을 대신 TableVIewController : UIViewController <UITableViewDelegate, UITableViewDataSource>을 사용하고 있습니까? 이보기에 다른 내용을 표시하고 있습니까? 그렇지 않은 경우에는 사용자가 가지고있는 것과 가장 가까운 뷰 컨트롤러를 서브 클래스 화해야합니다 (UITableViewController).

이렇게하면이보기의 기본 표는 tableView (또는 self.tableView, 필수는 아니지만 필수) 속성을 통해 액세스 할 수 있습니다. 그것은 당신의 문제의 원인이 아니지만 그것은 좋은 습관입니다.

관련 문제