2017-06-13 3 views
0

enter image description here、私はテーブルの値は、ミスの配置(ピッカーから選択した値)

あるスクロールするとここで私はUIPickerViewでダイナミックUITableViewCellをやろうとしています。
手順1: カスタムセルでは、1つのラベルと1つのUITextFieldを使用しました。
ステップ2: は、データ表示とデータフェッチのためにdownpickerviewライブラリを使用しました。
手順3: 以下のコードを使用してデータを選択できますが、それ以降はスクロールするとUITableViewのデータが配置されなくなります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"Cell"; 

    customCell *cell1=[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell1 == nil) 
    { 
     cell1 = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; 
    } 

    cell1.productTitleLabel.text =[arrProductTitle objectAtIndex:indexPath.row]; 

    self.downPicker = [[DownPicker alloc] initWithTextField:cell1.productvalueTextfield withData:arrProductVal]; 

    [self.downPicker addTarget:self action:@selector(measurementSelected:) forControlEvents:UIControlEventValueChanged]; 
    [cell1.contentView addSubview:self.downPicker]; 

    return cell1; 
} 

私を助けてください。

答えて

0

UITableViewCell他のセルのデータを設定していないため、値が誤って配置されています。値を配列に格納してそこから値を更新するだけで済みます。 :)

0

何かがあなたのコードを持つ権利ではありません。

cell1.productTitleLabel.text =[arrProductTitle objectAtIndex:indexPath.row]; 

self.downPicker = [[DownPicker alloc] initWithTextField:cell1.productvalueTextfield withData:arrProductVal]; 

[self.downPicker addTarget:self action:@selector(measurementSelected:) forControlEvents:UIControlEventValueChanged]; 
[cell1.contentView addSubview:self.downPicker]; 

ベア心​​の中のiOSには、のtableViewは、そのセルを再利用すること。したがって、UITableViewをスクロールすると、以前に作成された古いセルが再利用されます。
[cell1.contentView addSubview:self.downPicker];セルが再利用されるたびに実行されるため、1つのセルに多くのdownPickerオブジェクトが存在します。

+0

私はまた、あなたが、このためのソリューションを持っているなら、私に知らせて、同じ感じました。前もって感謝します。 –

+0

DownPickerをセル自体(customCellクラスのinitメソッド内)に直接追加してから、セル内にデリゲートを宣言し、VCをセルのデリゲートとして設定する必要があります。次に、downPickerが選択/タップされるたびに、デリゲートメソッドを呼び出して、セルのindexpathを使用してイベントを処理します。 –

+0

私はそれがうまくいけば解決策を見つけましたが、それは別のアプローチです。 –

0
- (void)viewDidLoad { 
[super viewDidLoad]; 
self.navigationItem.title = @"DETAILS"; 
_dict = [[NSMutableDictionary alloc]init]; 
arrProductTitle = [[NSMutableArray alloc]initWithObjects:@"title0",@"title1",@"title2",@"title3",@"title4",@"title5",@"title6",@"title7",@"title8",@"title9",@"title10",@"title11",@"title12",@"title13",@"title14",@"title15", nil]; 
arrProductVal = [[NSMutableArray alloc]initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",nil];} 

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *MyIdentifier = @"Cell"; 

customCell *cell1=[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

if (cell1 == nil) 
{ 
    cell1 = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; 
} 

cell1.productTitleLabel.text =[arrProductTitle objectAtIndex:indexPath.row]; 
NSLog(@"%@",_dict); 
NSLog(@"%@",[NSString stringWithFormat:@"%ld",(long)indexPath.row]); 

if (_dict[[NSString stringWithFormat:@"%ld",(long)indexPath.row]]) { 
    cell1.productvalueTextfield.text =[_dict valueForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]]; 
} 
else { 
    cell1.productvalueTextfield.text = @""; 
    self.downPicker = [[DownPicker alloc] initWithTextField:cell1.productvalueTextfield withData:arrProductVal]; 
    self.downPicker.tag = indexPath.row; 

    [self.downPicker addTarget:self action:@selector(measurementSelected:) forControlEvents:UIControlEventValueChanged]; 
    [cell1.contentView addSubview:self.downPicker]; 
} 
return cell1;} 

-(void)measurementSelected:(id)dp { 
NSString* selectedValue = [dp text]; 
NSString* selectedIndex = [NSString stringWithFormat:@"%ld",(long)[dp tag]]; 
[_dict setValue:[dp text] forKey:selectedIndex]; 
NSLog(@"_dict: %@",_dict); 

NSLog(@"SELECTED TAG:::::::%ld",[dp tag]); 
NSLog(@"SELECTED VALUE:::::::%@",selectedValue); 
NSLog(@"SELECTED INDEX VALUEEEEEEEEEEE:::::::%ld",[dp selectedIndex]);} 

https://github.com/gvniosdev/Dynamic-UItableview-with-Picker-Selection

関連する問題