将信息从文本字段加载到表视图

时间:2013-05-29 22:27:38

标签: objective-c uitableview uitextfield

您好我正在尝试制作一个包含文本字段,tableview和按钮的应用。 按下按钮时,它会将文本字段中的信息添加到de tableview。 有什么人知道什么时候能找到这个教程吗?我有一本书,但我找不到。 提前致谢。 到目前为止,这是我的代码:

代码.h

@interface BlockNotasViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *tableNota;}

@property (strong, nonatomic) IBOutlet UITextField *textonota;
@property (nonatomic, retain) IBOutlet UITableView *tableNota;
@property (nonatomic, strong) NSMutableArray * arrayNota;
- (IBAction)AddNota:(id)sender;
@end
<。pm的代码

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

// Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning
   {
    [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
  - (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
 }
 - (IBAction)AddNota:(id)sender {
[arrayNota addObject: textonota.text];
[tableNota reloadData];

}

//TableView------------------------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return [arrayNota count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}
 - (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] init];
}
cell.textLabel.text = [arrayNota objectAtIndex: indexPath.row];
return cell;
}
//----------------------------------------------

@end

没有更多错误,但按钮没有做任何事情

2 个答案:

答案 0 :(得分:0)

您可以设置一个NSMutableArray属性,其中包含要添加到tableView的所有单元格。

按下该按钮后,您将创建一个带有标记的单元格,并将该单元格的文本设置为UITextField的文本:

 cell.textLabel.text = aUITextField.text;

然后将单元格添加到NSMutableArray中,并调用[tableView reloadData]刷新表视图。

在cellForRowAtIndexPath:indexPath委托中,您可以使用该标记来确定要返回的NSMutableArray中的哪个单元格,例如:

[return [self.cellArray objectAtIndex:indexPath.row]];

希望这有帮助! :)

答案 1 :(得分:0)

你应该使用字符串保持可变数组。并使用此数组作为数据源的tableview的填充内容。在委托方法 - (UITableViewCell *)tableView:cellForRowAtIndexPath:如果需要,你将创建UITableViewCell:

- (UITableViewCell*) tableView: (UITableView*) tableView
         cellForRowAtIndexPath: (NSIndexPath*) indexPath
{ 
    UITableViewCell* cell = [tableViewdequeueReusableCellWithIdentifier: @"identifier"]; 
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
                                       reuseIdentifier: @"identifier"] autorelease];
    }
    cell.textLabel.text = [myMutableArray objectAtIndex: indexPath.row]; 
    return cell;
}

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

- (IBAction) pressButton: (id) sender
{
    [myMutableArray addObject: textField.text];
    [myTableView reloadData];
}

iOS开发人员的每本书都包含有关UITableView的信息。 请阅读https://stackoverflow.com/questions/3403049/best-book-resources-for-learning-ios-programming