所以我有两个视图,第一个有我的TableView,第二个有我的TextField,我想在我的TableView中用TextField中的文本添加一行。
目前我可以添加
行[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];
}
答案 0 :(得分:1)
我不确定您的问题究竟是什么,但我认为如果您向我们展示您的表格视图委托的-tableView:numberOfRowsInSection:
和-tableView:cellForRowAtIndexPath:
方法会更清楚。这些是您对表格视图行为进行更改的关键点,这是您的答案可能开始的地方。
好。这有点令人困惑 - 看起来myTableView
是NSArray
?通常,具有类似名称的变量应该是指向表视图的指针。但UITableView
既没有-addObject:
方法也没有-count
方法。
如果是这种情况,看起来你很好(虽然我真的认为你应该重命名那个数组)。您应该在UITableView上调用其中一个-reload*
方法,让它知道数据已更改。最简单的是
[ptrToTableView reloadData];
但是通过更多的工作,您可以使用-reloadSections:withRowAnimation:
获得更好的结果。
如果这不回答问题,那么我不确定问题是什么。 :)
答案 1 :(得分:0)
将头文件添加到它现在应该的UITextFieldDelegate
中:
@interface yourViewController : UIViewController <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> {
我们只是通过使用委托让ViewController识别使用TextField执行的操作。为了告诉textField,它的委托是,你必须在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 = @"";
}