简体   繁体   English

单击按钮时将表视图数据存储在数组中-目标c

[英]Store table view data in array on button click - Objective c

I have a UITableView with each cell having text fields. 我有一个UITableView ,每个单元格都有文本字段。 I want to get the text written in those text field in an NSMutableArray 我想在NSMutableArray中的那些文本字段中写入文本

Currently am doing this on button click - 当前正在通过单击按钮执行此操作-

- (void)viewDidLoad {
    [super viewDidLoad];
   _optionTextArray = [[NSMutableArray alloc] init];

}

- (IBAction)saveClicked:(UIButton *)sender {

    editVotingOptionCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"cell"];

    for (int i=0; i<_optionArray.count; i++) {
        [_optionTextArray addObject:cell.tfOption.text];
    }
}

But every time empty string is getting stored. 但是每次存储空字符串时。

@interface MyCell: UITableViewCell {
    @property(strong) UITextField *textField;
}

Suppose you have a cell with class MyCell when your button is tap first get the number of rows in section. 假设您首先单击按钮时有一个类为MyCell的单元格,则获得该节中的行数。 Let you have a default section which is 0 so get the number of rows for section zero after this iterate through a loop and get cell at every indexPath and get the text from that indexPath and check whether text is empty or not and finally insert it into an Array. 让您拥有一个默认的部分,该部分的默认值为0因此在循环遍历后获取部分零的行数,并在每个indexPath处获取单元格,并从该indexPath中获取文本,并检查文本是否为空,最后将其插入到数组。 Bellow is the sample code of button click. 波纹管是按钮单击的示例代码。

    NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0]
    NSMutableArray *allTexts = [NSMutableArray new];
    for (NSInteger i = 0; i< numberOfRows; i++) {
         NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
         MyCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        if (cell.textField.text != nil) {
            [allText addObject:cell.textField.text];
        }

}

You can try 你可以试试

(IBAction)saveClicked:(UIButton *)sender {

     for (int i=0; i<_optionArray.count; i++) {

        editVotingOptionCell *cell = [_tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:i inSection:0]];
        if (cell != nil) {
          [_optionTextArray addObject:cell.tfOption.text];
        }
        else {
          NSLog(@"nil cell")
        }

    }
}

// //

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { 
   editVotingOptionCell*cell = ///
   cell.tfOption.delegate = self;
   cell.tfOption.tag = indexPath.row;
   return cell;
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
   _optionTextArray[textField.tag] = textField.text 
}

// //

@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

Note: pre-init the array _optionTextArray with default values 注意:使用默认值预初始化数组_optionTextArray

If all cells are visible, as you said, you can do it like here: 如您所说,如果所有单元格都是可见的,则可以像下面这样进行操作:

- (IBAction)saveClicked:(UIButton *)sender {
    for (int i=0; i < _tableView.visibleCells.count; i++) {
       editVotingOptionCell *cell = self.tableView.visibleCells[i];
       [_optionTextArray addObject: cell.tfOption.text];
    }
}

UPDATE: 更新:

This method usable only if all your cells visible. 仅当所有单元格都可见时,此方法才可用。

Because of reusing you can't get textField in the binding to concrete cell. 由于重用,您无法在绑定到具体单元格的过程中获得textField。 Table view reuse cells. 表格视图重用单元格。

So advice that you can see below that with cellForRowAtIndexPath:indexPath is not good idea because of table view policy. 因此,由于表视图策略,建议您在下面看到关于cellForRowAtIndexPath:indexPath的建议不是一个好主意。

If you want to get all texts from cells you should save it in array in other place, not in table view cells. 如果要从单元格中获取所有文本,则应将其保存在数组中的其他位置,而不是表视图单元格中。 For example, you can save text from text field in UITextFieldDelegate method after text field update. 例如,您可以在文本字段更新后将文本字段中的文本保存在UITextFieldDelegate方法中。

I think it's better to get data from the tableView's dataSource array rather than from cells. 我认为最好是从tableView的dataSource数组中获取数据,而不是从单元格中获取数据。

So everytime textField's text changed you shoule store it's value,I had written a demo for you: 因此,每次textField的文本更改时,您都应该存储它的值,我为您编写了一个演示:

This is MyCell.h code: 这是MyCell.h代码:

#import <UIKit/UIKit.h>

typedef void(^TextFieldValueChangedBlock)(NSString *text);

@interface MyCell : UITableViewCell

@property (nonatomic, copy) TextFieldValueChangedBlock textFieldValueChangedBlock;

@property (nonatomic, copy) NSString *textFieldValue;

@end

This is MyCell.m code: 这是MyCell.m代码:

#import "MyCell.h"

@interface MyCell ()

@property (nonatomic, strong) UITextField *myTextField;

@end

@implementation MyCell 

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
        [self.contentView addSubview:self.myTextField];
        [self.myTextField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return self;
}

- (void)textFieldChanged:(UITextField *)textField {
    if (self.textFieldValueChangedBlock) {
        self.textFieldValueChangedBlock(textField.text);
    }
}

- (void)setTextFieldValue:(NSString *)textFieldValue {
    self.myTextField.text = textFieldValue;
}

@end

At last this is ViewController.m code: 最后,这是ViewController.m代码:

#import "ViewController.h"
#import "MyCell.h"

@interface ViewController () <UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSMutableArray *stringArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.dataArray = [NSMutableArray array];
    for (int i = 0; i < 100; i++) {
        [self.dataArray addObject:@"text"];
    }

    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
}

- (void)saveClicked:(UIButton *)sender {
    self.stringArray = self.dataArray.mutableCopy;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellReuseID = @"cell";
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseID];
    if (!cell) {
        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseID];
    }
    cell.textFieldValue = self.dataArray[indexPath.row];
    // textField value change,store value
    __weak typeof(self) weakSelf= self;
    cell.textFieldValueChangedBlock = ^(NSString *text) {
        __strong typeof(self) strongSelf = weakSelf;
        [strongSelf.dataArray replaceObjectAtIndex:indexPath.row withObject:text];
    };
    return cell;
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM