1

在我看来,我希望在右上角有一个添加按钮,当我们单击它时,它会提供带有文本字段的警报视图。当我们添加数据并单击 alertview 上的添加按钮时,应使用用户输入的文本创建一个新单元格。

我的代码:

-(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;
}

请帮助我在底部的表格中添加新单元格。

4

3 回答 3

2

您可以使用此方法插入行,试试这个

[self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationLeft];
于 2012-04-14T10:06:03.780 回答
2

如果要插入新单元格,insertRowsAtIndexPath则必须首先更新表的数据源。此外,此方法应在以下之间调用:

[table beginUpdates];

[table endUpdates];

从此:_

beginUpdates请注意在由and方法定义的动画块中调用此方法时的行为endUpdates。UITableView 将任何行或节的插入推迟到它处理了行或节的删除之后。无论插入和删除方法调用的顺序如何,都会发生这种情况。这与在可变数组中插入或删除项目不同,其中操作会影响用于连续插入或删除操作的数组索引。

于 2012-04-14T11:19:29.847 回答
1

在数组或字典中添加新值(无论您使用什么来显示 cell.textlable.text),然后使用 UitableView 的重新加载数据方法。

[MyTableView 重载数据];

于 2012-04-14T09:33:32.057 回答