UITextField を含む別のクラス (CustomCell) で定義されたカスタム プロトタイプ セルがある UITableView があります。ボタンを押すたびに、新しいセルを作成する addItem というメソッドが呼び出されます。UITextFields のテキストを配列に入れたい。よりよく説明するために、UITableView に 3 つのセルを追加し、対応する UITextFields に 3 つのテキストを入力すると、最初のセルのテキストはインデックス 0 の配列に移動し、2 番目のセルのテキストはインデックス 1 に移動します私の最大の問題は、セル 1 の UITextField に戻って更新し、それに対応する NSArray オブジェクトを動的に更新できるようにしたいということです。インデックス 0 に 1 つ。アプローチ方法がわかりません。誰か助けてくれませんか??? どうもありがとうございました!!
私のコード(obs:itemTableはUITableViewです):
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:
- (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
@interface CustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *itemNumber;
@property (strong, nonatomic) IBOutlet UITextField *itemValue;
@end