How do I replace the date with a writable title?

做~自己de王妃 提交于 2019-12-11 16:21:41

问题


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:


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

}


来源:https://stackoverflow.com/questions/11163341/how-do-i-replace-the-date-with-a-writable-title

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!