从文本字段加载信息到表视图
问题描述:
嗨我想做一个应用程序有一个文本字段,tableview和一个按钮。 当您按下按钮时,它会将文本字段中的信息添加到tableview中。 做任何人知道我什么时候可以找到这个教程?我有一本书,但我无法找到它。 在此先感谢。 这是到目前为止我的代码:.M的从文本字段加载信息到表视图
- (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
的.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
代码
代码
没有更多的错误,但按钮没有做任何事
答
您可以设置一个NSMutableArray属性,其中包含所有要添加到tableView的单元格。
当按下该按钮,创建一个标签的细胞,而细胞集文字到您的UITextField的文字:
cell.textLabel.text = aUITextField.text;
再增加该小区成的NSMutableArray,并调用[的tableView reloadData]刷新表视图。
在的cellForRowAtIndexPath:indexPath委托,您可以使用该标记来确定哪个单元的NSMutableArray返回,例如:
[return [self.cellArray objectAtIndex:indexPath.row]];
希望这有助于! :)
答
你应该保持可变数组与字符串。并使用此数组填充tableview的内容作为数据源。在委托方法 - (UITableViewCell的*)的tableView:的cellForRowAtIndexPath:
- (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的信息:如果需要,这样你将创建的UITableViewCell。 请阅读https://*.com/questions/3403049/best-book-resources-for-learning-ios-programming
是的,我给了我一个如何做的想法,但你知道我可以遵循的指南吗? – darkjuso
我遇到麻烦的事情是与单元格的数组和什么要放在错误按钮 – darkjuso
,你确定当前类是UITableView的子类吗?看起来像UIView接收UITableView的委托方法。这里是UITableView的教程:http://www.iosdevnotes.com/2011/10/uitableview-tutorial/ – coolbeet