2012-04-14 115 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的 推迟行或部分的任何插入,直到后它已处理 行或部分缺失。无论订购 的插入和删除方法调用如何,都会发生这种情况。这不同于插入 或在一个可变的阵列,其中该操作可能会影响 用于连续插入或移除 操作所述阵列索引中删除的项。

相关问题