0

当我添加新行时,它当前会添加一个以当前日期为标题的新行。以下是我拥有的代码。我应该将“NSDate date”更改为什么,以便用户可以输入他们想要的任何标题?

(void)insertNewObject:(id)sender
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
4

1 回答 1

0

您可以将 newUIAlertView与纯文本输入一起使用。您还必须更改表格视图单元格以显示一个NSString而不是一个NSDate. (哦,你必须UIAlertViewDelegate在课堂上采用。

-(void)insertNewObject:(id)sender
{

    UIAlertView * getTitle = [[UIAlertView alloc] initWithTitle:@"title" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; // should give proper cancel/accept buttons
    getName.alertViewStyle = UIAlertViewStylePlainTextInput;
    [getTitle show];
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }

    NSString * userEnteredThisString = [[alertView textFieldAtIndex:0] text];
    [_objects insertObject:userEnteredThisString atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}
于 2012-06-22T20:21:53.643 回答