簡體   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