2011-09-20 68 views
0

我創建了一個表格有了一些選項列表,其中一個是「添加新行」。如果用戶點擊它,他將得到一個警報視圖.i使用文本字段自定義該警報視圖。但問題是我無法將輸入到文本字段中的數據添加到我的表格視圖中。如何從警報視圖添加數據到我的表

有人請幫忙。

- (void)tableView:(UITableView *)aTableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

if (editingStyle == UITableViewCellEditingStyleDelete) 
{ 
    [self.listOfCategories removeObjectAtIndex:indexPath.row]; 
    [self.tableViewC reloadData]; 
} 
else if (editingStyle == UITableViewCellEditingStyleInsert) 
{ 
    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Your title here!" message:@"...." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
    myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
    [myTextField setBackgroundColor:[UIColor whiteColor]]; 
    [myAlertView addSubview:myTextField]; 
    [myAlertView show]; 
    [myAlertView release]; 
    [self.listOfCategories insertObject:@"Add" atIndex:[self.listOfCategories count]]; 
    [self.tableViewC reloadData]; 
} 
+1

也許表現出一定的代碼,你到目前爲止,你已經嘗試把文本在表格等 – ophychius

+2

你必須使用UIAlertViewDelegate方法。查看相關文檔。 – vikingosegundo

+0

這裏是我卡住....我不知道下一步該怎麼做 – Chandu

回答

0

你alertView委託編寫代碼..

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 
     if(buttonIndex==0) 
     { 
       NSIndexPath *newPath = [NSIndexPath indexPathForRow:yourArray.count inSection:0]; 
       [yourArray insertObject:[NSString stringWithFormat:@"%@",yourTextField.text] atIndex:newPath.row]; 
       [tableView reloadData]; 
     } 
    } 
0

不要以爲它會添加一行到您的表視圖。把它想象成添加一個條目到你的數據源。你如何存儲你的數據?如果它是一個數組,請將該對象添加到數組中,然後重新加載您的表。

相關問題