iOS:从 UITableView 动态添加数据到 NSArray [英] iOS: dynamically adding data from UITableView to NSArray

查看:40
本文介绍了iOS:从 UITableView 动态添加数据到 NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView,其中有一个自定义原型单元格,在另一个类 (CustomCell) 中定义,其中包含一个 UITextField.每次按下按钮时,它都会调用一个名为 addItem 的方法,该方法会创建一个新单元格.我希望 UITextFields 中的文本转到数组.为了更好地解释它,如果我向 UITableView 添加 3 个单元格并在相应的 UITextFields 中输入 3 个文本,我希望第一个单元格中的文本转到索引 0 中的数组,第二个单元格中的文本转到索引 1和第 3 个单元格中的文本转到索引 2.我最大的问题是我希望能够回到单元格 1 中的 UITextField 并更新它,并让它动态更新与其对应的 NSArray 对象,即索引 0 处的一个.我不知道如何处理它.有人可以帮忙吗???非常感谢!!

I have a UITableView in which I have a custom prototype cell, defined in another class (CustomCell), with a UITextField in it. Every time I press a button, it calls a method called addItem, which creates a new cell. I want the texts in the UITextFields to go to an array. To try to explain it better, if I add 3 cells to the UITableView and input 3 texts in the corresponding UITextFields, I want the text in 1st cell to go to the array in index 0, the text in the 2nd to go to index 1 and the text in 3rd cell to go to index 2. My biggest problem is that I want to be able to go back to UITextField in cell 1 and update it, and have it dynamically update the NSArray object corresponding to it, that is, the one at index 0. I have no idea how to approach it. Can anybody help??? Thank you very much!!

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

my code (obs: itemTable is the UITableView):

MainViewController.m

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:

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

CustomCell.h

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

@end

推荐答案

当用户输入完文本后,您可以执行如下操作,将 tableview 中行的索引路径映射到数组中的索引.

When the user is done entering text you could do something like the following which maps the index paths of the rows in your tableview to the indices in an array.

- (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,当用户完成输入文本时,您可以执行以下操作:

Assuming your cell has the UITextFieldDelegate set, when the user is done entering text you can do something like this:

- (void)textFieldDidEndEditing:(UITextField *)textField {

    [self.delegate didFinishEditing];
}

其中 self.delegate 是 UITableViewController,它会在必要时调用 updateText.

Where self.delegate is the UITableViewController, which in turn call updateText when necessary.

需要注意的事情 - updateText 中的 for 循环需要遍历 tableview 并为每个索引路径出列单元格.简单地使用 tableview 的可见单元格很可能会让您丢失屏幕外的单元格中的文本并被重用.

Things to be careful of - the for loop in updateText needs to loop over the tableview and dequeue cells for each index path. Simply using the tableview's visible cells would most likely leaving you missing text from cells that were off screen and got reused.

希望这会有所帮助并祝您好运!

Hope this helps and good luck!

这篇关于iOS:从 UITableView 动态添加数据到 NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆