iOS:从UITableViewdynamic添加数据到NSArray

我有一个UITableView中,我有一个自定义原型单元格,在另一个类(CustomCell)中定义,其中有一个UITextField。 每次按下button,它都会调用一个名为addItem的方法,它会创build一个新的单元格。 我想UITextFields中的文本去一个数组。 为了更好地解释它,如果我将3个单元格添加到UITableView中,并在相应的UITextFields中input3个文本,我希望第一个单元格中的文本转到索引0中的数组,第二个中的文本转到索引1和第三个单元格中的文本去索引2.我最大的问题是,我希望能够回到单元格1中的UITextField并更新它,并让它dynamic更新与它对应的NSArray对象,也就是一个在索引0.我不知道如何处理它。 任何人都可以帮助? 非常感谢你!!

我的代码(obs:itemTable是UITableView):

MainViewController.m

@implementation addViewController{ NSInteger n; NSString *aid; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } -(void)viewWillAppear:(BOOL)animated{ n=1; aid=@""; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return n; } - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier= @"Cell"; CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } cell.itemNumber.text=[NSString stringWithFormat:@"Item %d",indexPath.row]; return cell; } - (IBAction)addItem:(UIButton *)sender { ++n; [_itemTable reloadData]; } - (IBAction)removeItem:(UIButton *)sender { if (n>=0)--n; [_itemTable reloadData]; } 

CustomCell.m:

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) {_itemValue = [[UITextField alloc]init]; _item = [[UILabel alloc]init]; [self.contentView addSubview:_itemValue]; [self.contentView addSubview:_item]; } return self; } 

CustomCell.h

 @interface CustomCell : UITableViewCell @property (strong, nonatomic) IBOutlet UILabel *itemNumber; @property (strong, nonatomic) IBOutlet UITextField *itemValue; @end 

当用户完成input文本时,可以执行类似下面的操作,将tableview中的行的索引path映射到数组中的索引。

 - (NSMutableArray *)updateText { NSUInteger cellsCount = [self.tableView numberOfRowsInSection:0]; NSMutableArray *cellTextArray = [[NSMutableArray alloc] initWithCapacity:cellsCount]; for(NSInteger i = 0; i < cellsCount; i++) { NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0]; CustomCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; NSString *item = cell.itemNumber.text; [cellTextArray insertObject:item atIndex:i]; } return cellTextArray; } 

假设你的单元格有UITextFieldDelegate集合,当用户input文字时,你可以做这样的事情:

 - (void)textFieldDidEndEditing:(UITextField *)textField { [self.delegate didFinishEditing]; } 

其中self.delegate是UITableViewController,它在需要时又调用updateText

需要注意的是 – updateText的for循环需要遍历tableview,并为每个索引path出列单元格。 简单地使用tableview的可见单元格很可能会让您遗漏来自屏幕外的单元格的文本并被重用。

希望这有助于和祝你好运!

首先,当你创build每个文本字段时,你可以创build自己的文本字段的委托,所以无论何时在文本字段中发生任何事情,都会收到消息。

好吧,现在当用户input一个文本字段时,你会得到消息,并且你可以修改你的模型(数组,我们应该把它保留为一个NSMutableArray)。 但要做到这一点,你需要弄清楚哪个heck单元格包含这个消息来自的文本字段! 你会这样做:

 - (void)textFieldDidEndEditing:(UITextField *)tf { // some cell's text field has finished editing; which cell? UIView* v = tf; do { v = v.superview; } while (![v isKindOfClass: [UITableViewCell class]]); CustomCell* cell = (CustomCell*)v; // so which row is it? NSIndexPath* ip = [self.tableView indexPathForCell:cell]; // aha! so now ip.row is the row, and you can update your data model // ... left as an exercise for the reader ... } 

我在我的书中做了这样的事情,在http://www.apeth.com/iOSBook/ch21.html#_editable_content_in_table_items (这是上面的代码来自哪里),所以看一下,看看它给你什么想法。

这个问题显然有几个方面。 首先,你想能够恢复对UILabel的引用,这样你就可以找出一个特定的UILabel所在的行。我build议使用tag属性来做这件事,如下所示:

 _item = [[UILabel alloc] init]; _item.tag = 100; // or any value [self.contentView addSubview:_item]; 

您还需要设置一个在标签中的文本发生变化时被调用的操作。 你可以这样做:

 [_item addTarget:someObject action:@selector(labelChanged:) forControlEvents:UIControlEventEditingChanged]; 

无论类someObject是什么类,它都需要一个具有这个签名的方法:

 - (void)labelChanged:(id)sender; 

在这个方法里面,你可以检查sender实际上是一个UILabel ,然后你可以用sender.text访问新的文本。

为了找出数组中什么点放置文本,可以在表中的行数上声明一个循环:

 for (int i = 0; i < [mainViewControllerInstance tableView:theTableInQuestion numberOfRowsInSection:0]; ++i) { if(sender == [[mainViewControllerInstance tableView:theTableInQuestion cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] viewWithTag:100]) { // Put `sender.text` in the appropriate spot in your array } } 

一些附加说明:我会使用一个NSMutableArray来跟踪你的string,因为你会更新它们,但我不完全确定什么是最佳实践。 你还需要确保你初始化你的数组(无论你是NSArray还是NSMutableArray )才能拥有正确的行数,并且确保当你按下+button的时候向它添加行,当您尝试更改尚不存在的行的条目时,您将冒风险。

你可能也想看看免费的Sensible TableView框架。 框架几乎可以自动执行所有你需要的东西。 应该可能为您节省大量的体力劳动。 祝你好运!