2012-04-14 96 views
1

在我看來,我想要一個右上角的添加按鈕,當我們點擊它時,它會給出帶有文本字段的警報視圖。當我們添加數據並在alertview上單擊添加按鈕時,應該使用用戶輸入的文本創建一個新單元格。在點擊添加按鈕時在uitableview底部添加一個新單元格

我的代碼:

-(IBAction)addNewBreed:(id)sender{ 
    UIAlertView *addAlert=[[UIAlertView alloc]initWithTitle:@"Add New Breed" message:@"\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add",nil]; 
    UITextField *addTextField=[[UITextField alloc]initWithFrame:CGRectMake(12,45,260,25)]; 
    [addTextField becomeFirstResponder]; 
    [addTextField setBackgroundColor:[UIColor whiteColor]]; 
    addTextField.clearButtonMode=UITextFieldViewModeWhileEditing; 
    [addAlert addSubview:addTextField]; 
    [addAlert show]; 
    [addAlert release]; 

} 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 

NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex]; 

if ([buttonTitle isEqualToString:@"Add"]) { 

    [treeTable insertRowsAtIndexPaths:[NSArray arrayWithObject:addIndexPath] withRowAnimation:UITableViewRowAnimationBottom]; 
} 
else 
     return; 
} 

請幫我在底部添加新小區表。

+0

由於以下原因,我的應用程序崩潰:***由於未捕獲的異常'NSInternalInconsistencyException',原因:'無效的更新:無效的行數在第0節中的行終止應用程序。 update(8)必須等於更新前(8)的該部分中包含的行數,加上或減去從該部分插入或刪除的行數(插入1個,刪除0個)。 – anjum 2012-04-14 09:40:15

+0

什麼是您的UITableViewDataSource,並且當您向該表中添加一行時,是否向其中添加了一個元素? – 2012-04-14 11:46:15

回答

1

在數組或字典中添加新值(無論您用於顯示cell.textlable.text)並僅使用UitableView的重載數據方法。

[MyTableView ReloadData];使用這種方法

+0

但是沒有這樣的動畫。 – Mat 2012-04-14 11:22:53

2

可以插入行,試試這個

[self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
2

如果你想插入一個新的細胞與insertRowsAtIndexPath你必須先更新表的數據源。此外,這種方法應該之間被調用:

[table beginUpdates]; 

​​

this

注意這種方法的調用時在由所定義的動畫 塊中的行爲beginUpdatesendUpdates方法。 UITableView的 推遲行或部分的任何插入,直到後它已處理 行或部分缺失。無論訂購 的插入和刪除方法調用如何,都會發生這種情況。這不同於插入 或在一個可變的陣列,其中該操作可能會影響 用於連續插入或移除 操作所述陣列索引中刪除的項。

相關問題