2010-12-30 10 views
1

私は配列とテーブルビューを使って本当に簡単なリストを作成しようとしています。ユーザーが+ボタンを押すと、挿入メソッドが呼び出され、ユーザーがテキストを入力できるようにUIAlertが表示されます。何もクラッシュしませんが、ユーザーがOKを押すと、何もテーブルビューに追加されませんが、ログにはそのコードが表示されます。誰が何が間違っているかを検出できますか?あなたは、配列を変更した後にそのデータを再ロードするためにテーブルビューを強制する必要がUITableViewとNSArrayのヘルプ

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    data=[[NSMutableArray alloc]init]; 

    // Override point for customization after application launch. 

    [self.window makeKeyAndVisible]; 

    return YES; 
} 

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

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

} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if(cell==nil) 
     cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]autorelease]; 
    cell.textLabel.text=[data objectAtIndex:indexPath.row]; 
    return cell; 
} 

-(IBAction)insert { 
    UIAlertView* dialog = [[UIAlertView alloc] init]; 
    [dialog setDelegate:self]; 
    [dialog setTitle:@"Enter Name"]; 
    [dialog setMessage:@" "]; 
    [dialog addButtonWithTitle:@"Cancel"]; 
    [dialog addButtonWithTitle:@"OK"]; 

    UITextField *nameField = [[UITextField alloc] 
           initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)]; 
    [nameField setBackgroundColor:[UIColor whiteColor]]; 
    [dialog addSubview:nameField]; 
    [nameField release]; 
    [dialog show]; 
    [dialog release]; 
} 


- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{  
    if (buttonIndex != 0) // 0 == the cancel button 
    { 
     // Since we don't have a pointer to the textfield, we're 
     // going to need to traverse the subviews to find our 
     // UITextField in the hierarchy 
     for (UIView* view in alertView.subviews) 
     { 
      if ([view isKindOfClass:[UITextField class]]) 
      { 
       UITextField* textField = (UITextField*)view; 
       [data addObject:textField.text == nil ? @"moha" : textField.text]; 
       NSLog(@"text:[%@]", textField.text); 
       break; 
      } 
     } 
    } 
} 
-(IBAction)toggleEdit { 
    UIBarButtonItem *leftItem; 
    [mainTableView setEditing:!mainTableView.editing animated:YES]; 

    if (mainTableView.editing) { 
     leftItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(toggleEdit)]; 
    }else { 
     leftItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(toggleEdit)]; 
    } 

    navItem.leftBarButtonItem=leftItem; 

} 

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [data removeObjectAtIndex:indexPath.row]; 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 

} 

答えて

2

(私は、私もIB内のすべての権利の接続を作ったと思う):

[self.tableView reloadData]; 
+0

はいああそれはそれでした!ありがとう! – Snowman

+0

それ以外は '[mainTableView reloadData];' – Snowman

関連する問題