Add row in a TableView

柔情痞子 提交于 2019-12-11 14:59:10

问题


So I have two views, the first have my TableView and the second have my TextField and I want to add a row in my TableView with the text in my TextField.

For the moment I can just add row with

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

Thanks for your help!

- (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:


I'm not sure exactly what your question is here but I think it will be much clearer if you show us your table view delegate's -tableView:numberOfRowsInSection: and -tableView:cellForRowAtIndexPath: methods. These are the key points where you'll make changes to your table view's behavior, and this is where your answer is likely to start.


Okay. This is a little confusing -- it looks like myTableView is an NSArray? Normally a variable with a name like that would be expected to be a pointer to a table view. But UITableView has neither an -addObject: method nor a -count method.

If that is the case, it looks like you're good (though I really think you should rename that array). You should call one of the -reload* methods on your UITableView to let it know that the data has changed. The simplest is

[ptrToTableView reloadData];

but with a little more work you can get fancier results with -reloadSections:withRowAnimation:.

If this doesn't answer the question, then I'm not sure what the question is. :)




回答2:


add to your header file the UITextFieldDelegate it should like now like:

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

What we simply do is, that we make your ViewController recognizing actions that are performed with your TextField, by using the delegate. In order to tell the textField, whose delegate it is, you have to write for example in the viewDidLoad-method of your ViewController:

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

So we have to implement the function, that if the user is done with editing, the new text is added:

#pragma mark -
#pragma mark  UITextFieldDelegate Methods

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

Further methods of the delegate are listed below: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html



来源:https://stackoverflow.com/questions/3496699/add-row-in-a-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!