を実際に使う必要がなかったUIStepper
ので、試してみようと思いました。
そして、表のセル内で試しました。以下は私のコードです:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIStepper *stepper = [[UIStepper alloc] initWithFrame:(CGRect){{40, 40}, 20, 20}];
[stepper addTarget:self action:@selector(stepperTapped:) forControlEvents:UIControlEventValueChanged];
stepper.tag = indexPath.row;
stepper.stepValue = 1;
stepper.continuous = YES;
stepper.minimumValue = 0;
stepper.maximumValue = 10;
[cell.contentView addSubview:stepper];
return cell;
}
アクション方法:
- (void)stepperTapped:(UIStepper*)stepper
{
NSLog(@"sender tag: %li", (long)stepper.tag);
UITableViewCell *currentCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:stepper.tag inSection:0]];
NSNumber *stepperValue = [NSNumber numberWithDouble:stepper.value];
currentCell.textLabel.text = [NSString stringWithFormat:@"%f", stepper.value];
[self.tableView reloadData];
}
4 つのセルと 1 つのセクションでテストしています。
実行されますが、ステッパーは 1 回だけインクリメントし (0 -> 1 または 1 -> 0)、設定した最大数である 10 まで上がりません。
テーブルセル内ではない同じコードをテストしましたが、正常に動作します。0 から 10 まで、10 から 0 まで、各ステップ。
この小さな実験で、解決策を見つけるのに 2 時間かかりました。
誰でもここで問題を見つけることができますか?