开发者

Add row in a TableView

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];

}


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. :)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜