2010-08-16 42 views
0

所以我有兩個視圖,第一個有我的TableView,第二個有我的TextField,我想用我的TextField中的文本在我的TableView中添加一行。在TableView中添加行

目前我可以只添加一行

[myTableView addObject:@"Test 1"]; 
[myTableView addObject:@"Test 2"]; 
[myTableView addObject:@"Test 3"]; 

感謝您的幫助!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

NSString *cellValue = [myTableView objectAtIndex:indexPath.row]; 
cell.textLabel.text = cellValue; 

return cell; 

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

return [myTableView count]; 

}

回答

1

我不知道你的問題在這裏到底是什麼,但我認爲這將是更清楚,如果你告訴我們,你的表視圖委託的-tableView:numberOfRowsInSection:-tableView:cellForRowAtIndexPath:方法。這些是您要對錶格視圖的行爲進行更改的關鍵點,這就是您的答案可能開始的地方。


好的。這有點令人困惑 - 它看起來像myTableViewNSArray?通常情況下,具有類似名稱的變量應該是指向表視圖的指針。但是UITableView既沒有-addObject:方法也沒有-count方法。

如果是這樣,它看起來像你很好(雖然我真的認爲你應該重新命名該數組)。您應該調用UITableView上的-reload*方法之一,讓它知道數據已更改。最簡單的是

[ptrToTableView reloadData]; 

但有一點更多的工作,你可以用-reloadSections:withRowAnimation:更大膽的嘗試結果。

如果這不能回答這個問題,那麼我不確定問題是什麼。 :)

+0

奧科克我有添加我的代碼 – Alex 2010-08-16 20:06:29

+0

奧科克感謝,但問題是,我的文本字段不與我TABL鏈接eView如何添加一行? – Alex 2010-08-16 20:30:58

+0

和myTableView是一個NSMutableArray – Alex 2010-08-16 20:33:19

0

添加到您的頭文件UITextFieldDelegate現在想這樣的:

@interface yourViewController : UIViewController <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> { 

我們只是做的是,我們讓你的ViewController承認與您的文本字段執行的操作,通過使用委託。爲了告訴文本框,其代表是,你在你的ViewController的viewDidLoad中的方法來寫,例如:

- (void) viewDidLoad 
{ 
    [super viewDidLoad]; 
    textField.delegate = self; 
} 

所以我們要實現的功能,如果用戶與編輯完成,新的文本添加:

#pragma mark - 
#pragma mark UITextFieldDelegate Methods 

- (void) textFieldDidEndEditing:(UITextField *)textField 
{ 
    [myTableView addObject:textField.text]; 
    [myTableView reloadData]; 
    textField.text = @""; 
} 

委託的其它方法列舉如下: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

相關問題