2012-06-22 101 views
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]; 
} 
+0

看起來你已經生成在Xcode默認MasterViewController項目。我會建議尋找在UITableView的教程或者是iOS上讀一本書發展,也許有人可以推薦一個。 –

回答

0

您可以使用純文本輸入的新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]; 

}