【问题标题】:How do I replace the date with a writable title?如何用可写标题替换日期?
【发布时间】:2012-06-22 20:04:47
【问题描述】:

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

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

【问题讨论】:

  • 看起来您已经在 Xcode 中生成了默认的 MasterViewController 项目。我建议搜索有关 UITableView 的教程,或者阅读有关 iOS 开发的书。也许有人可以推荐一个。

标签: iphone date title


【解决方案1】:

您可以使用带有纯文本输入的新 UIAlertView。您还必须更改表格视图单元格以显示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-22
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    相关资源
    最近更新 更多