代码之家  ›  专栏  ›  技术社区  ›  TheTravloper

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

  •  2
  • TheTravloper  · 技术社区  · 7 年前

    我有一个 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];
        }
    }
    

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

    4 回复  |  直到 7 年前
        1
  •  0
  •   Syed Qamar Abbas    7 年前
    @interface MyCell: UITableViewCell {
        @property(strong) UITextField *textField;
    }
    

    假设你有一个带班级的牢房 MyCell 当您的按钮是轻触第一次获得行数在节。让你有一个默认的部分 0

        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];
            }
    
    }
    
        2
  •  0
  •   Shehata Gamal    7 年前

    你可以试试

    (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

        3
  •  0
  •   Anton Rodzik    7 年前

    如果所有的细胞都是可见的,如你所说,你可以这样做:

    - (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。表视图重用单元格。

    所以建议你可以看到下面的cellForRowAtindexath:索引是这不是个好主意,因为表视图策略。

    如果你想从单元格中获取所有文本,你应该把它保存在另一个地方的数组中,而不是表格视图的单元格中。例如,您可以在文本字段更新后将文本字段中的文本保存在UITextFieldDelegate方法中。

        4
  •  0
  •   无夜之星辰    7 年前

    我认为最好从 tableViews数据源数组

    所以每次textFields文本改变你都应该存储它的值,我为你写了一个演示:

    这是 代码:

    #import <UIKit/UIKit.h>
    
    typedef void(^TextFieldValueChangedBlock)(NSString *text);
    
    @interface MyCell : UITableViewCell
    
    @property (nonatomic, copy) TextFieldValueChangedBlock textFieldValueChangedBlock;
    
    @property (nonatomic, copy) NSString *textFieldValue;
    
    @end
    

    迈西尔·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
    

    视图控制器.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