0

我已经更新了我的代码,

一切似乎都很好,但是当我按下添加按钮时,它并没有添加表格。

然后我意识到我忘了初始化数组,然后一切正常

#import "RootViewController.h"
#import "Hello_WorldAppDelegate.h"


@implementation RootViewController

@synthesize tableView;


- (void)viewDidLoad {
// Add the following line if you want the list to be editable
// self.navigationItem.leftBarButtonItem = self.editButtonItem;
}


- (IBAction)addToTable:(id)sender{

 /*  
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you want to say hello?" message:@"More info..." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Say  Hello",nil];
[alert show];
[alert release];

*/

[myArray addObject:textField.text];
[self.tableView reloadData];

textField.text=@"";

/*
static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [self.tableView  dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}

[cell setText:[textField text]];

[self.tableView reloadData];
*/
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return[myArray count];
}


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

static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero  reuseIdentifier:MyIdentifier] autorelease];
}

[cell setText:[myArray objectAtIndex:indexPath.row]];

// Set up the cell
return cell;
}


 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 // Navigation logic
}



// Override if you support editing the list
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}   
/*if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}*/ 
}



/*
 Override if you support conditional editing of the list
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/


/*
 Override if you support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath  toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
 Override if you support conditional rearranging of the list
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath   {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
 */ 


- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}


- (void)dealloc {
[textField release];
[addButton release];
[super dealloc];
}


- (void)viewDidUnload {
[textField release];
textField = nil;
[addButton release];
addButton = nil;
[super viewDidUnload];
}
@end

好的,我已经解决了我的问题

谢谢大家

4

3 回答 3

1

实际上问题在于您的方法名称。

- (IBAction)addToTable:(id)sender:(UITableView *)tableView 

像这样改变它

- (IBAction)addToTable:(id)sender
于 2013-06-25T07:43:35.953 回答
0

您需要什么,首先使用数组作为表格的数据源,然后单击您需要将 textField 内容添加到数组中的按钮,然后重新加载表格。

所以你的按钮动作看起来像

- (IBAction)addToTable:(id)sender
{
   [dataSouceArray addObject:yourTextField.text];
   [yourTable reloadData];

   yourTextField.text=@"";
}

和 tableViewDelegate 看起来像

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [yourDataSourceArray count];
}


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

static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero    reuseIdentifier:MyIdentifier] autorelease];
}

[cell setText:[yourDataSourceArray objectAtIndex:indexPath.row]];

// Set up the cell
return cell;
}
于 2013-06-25T07:50:56.790 回答
0

斯威夫特(3.0)

@IBAction func add(_ sender: Any) {

yourarrayname.append(yourtextfield.text!)

yourtableview.reloadData()

}

于 2017-02-22T07:49:45.633 回答