繁体   English   中英

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

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

我有一个UITableView ,每个单元格都有文本字段。 我想在NSMutableArray中的那些文本字段中写入文本

当前正在通过单击按钮执行此操作-

- (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];
    }
}

但是每次存储空字符串时。

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

假设您首先单击按钮时有一个类为MyCell的单元格,则获得该节中的行数。 让您拥有一个默认的部分,该部分的默认值为0因此在循环遍历后获取部分零的行数,并在每个indexPath处获取单元格,并从该indexPath中获取文本,并检查文本是否为空,最后将其插入到数组。 波纹管是按钮单击的示例代码。

    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];
        }

}

你可以试试

(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>

注意:使用默认值预初始化数组_optionTextArray

如您所说,如果所有单元格都是可见的,则可以像下面这样进行操作:

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

更新:

仅当所有单元格都可见时,此方法才可用。

由于重用,您无法在绑定到具体单元格的过程中获得textField。 表格视图重用单元格。

因此,由于表视图策略,建议您在下面看到关于cellForRowAtIndexPath:indexPath的建议不是一个好主意。

如果要从单元格中获取所有文本,则应将其保存在数组中的其他位置,而不是表视图单元格中。 例如,您可以在文本字段更新后将文本字段中的文本保存在UITextFieldDelegate方法中。

我认为最好是从tableView的dataSource数组中获取数据,而不是从单元格中获取数据。

因此,每次textField的文本更改时,您都应该存储它的值,我为您编写了一个演示:

这是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

这是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

最后,这是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