How do I replace the date with a writable title?

2019-08-16 15:07发布

When I add a new row, it currently adds a new row with the current date as the title. Below is the code that I have. What do I change "NSDate date" to so that the user can input whatever title they want?

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

1条回答
Melony?
2楼-- · 2019-08-16 15:44

You could use the new UIAlertView with plain text entry. You'll also have to change your table view cell to display an NSString instead of an NSDate. (Oh, and you have to adopt the UIAlertViewDelegate in the class.

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

}
查看更多
登录 后发表回答