gpt4 book ai didi

ios - UIStepper 没有更新相应的标签

转载 作者:行者123 更新时间:2023-12-01 18:00:08 25 4
gpt4 key购买 nike

我有一个 UIStepper我在 UITableViewCell 中以编程方式创建的控件.我还创建了一个 UILabel以编程方式为单元格。发生的事情是,按下时步进器正在访问 UILabel位于不同的单元格中,即索引路径 1 而不是 0。这是代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
...
//---------UIStepper Creation-------------------//

stepper = [[UIStepper alloc]init];
[stepper setFrame:CGRectMake(220.0f, 65.0f, 0.0f, 0.0f)];
[stepper setTag: indexPath.row];
[stepper addTarget:self action:@selector(stepperPressed:) forControlEvents:UIControlEventValueChanged];

stepperLabel = [[UILabel alloc] init];
[stepperLabel setFrame:CGRectMake(105,70, 20, 20)];
[stepperLabel setTextColor:[UIColor whiteColor]];
[stepperLabel setBackgroundColor:[UIColor clearColor]];
[stepperLabel setTag:stepper.tag];
[stepperLabel setText:[NSString stringWithFormat: @"%d", (int) [stepper value]]];
stepperLabel.lineBreakMode = UILineBreakModeWordWrap;
stepperLabel.numberOfLines = 1;//Dynamic

//Set min and max
[stepper setMinimumValue:0];
[stepper setMaximumValue:4];
// Value wraps around from minimum to maximum
[stepper setWraps:YES];
[stepper setContinuous:NO];

// To change the increment value for each step

...
[cell addSubview:stepperLabel];
[cell addSubview:stepper];
return cell;

这是我需要执行操作的地方
-(void) stepperPressed:(UIStepper *)sender{
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[sender superview]];
int row = indexPath.row;
NSLog(@"stepper is in row %d",row);
NSLog(@"stepperLabel is in row %d",stepperLabel.tag);
double value = [sender value];
if(sender.tag ==row){
[stepperLabel setText:[NSString stringWithFormat: @"%d", (int) value]];
}
}

真的需要一些帮助

最佳答案

stepperLabel 将始终指向您在 cellForRowAtIndexPath 中创建的最后一个 Cell 的标签。
所以你不能这样使用它。

编辑答案:

- (void)viewDidLoad {
[super viewDidLoad];
valueArray=[[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt:0],[NSNumber numberWithInt:0],[NSNumber numberWithInt:0], nil]; //Array Count =
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 10.0f, 100.0f, 21.0f)];
[cell addSubview:label];
[label setTag:456];

UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(110.0f, 10.0f, 20.0f, 20.0f)];
[cell addSubview:stepper];
[stepper setTag:123];
[stepper addTarget:self action:@selector(stepperChanged:) forControlEvents:UIControlEventValueChanged];
}
[cell setTag:indexPath.row];
int count = [[valueArray objectAtIndex:indexPath.row] intValue];

[(UIStepper*)[cell viewWithTag:123] setValue:count];
[(UILabel*)[cell viewWithTag:456] setText:[NSString stringWithFormat:@"%@: %d", @"Stepper", count]];
return cell;
}
- (void)stepperChanged:(UIStepper*)sender {
int row = [sender.superview tag];
int value = (int)[sender value];
NSLog(@"Stepper%d = %d", row,value);

[valueArray replaceObjectAtIndex:row withObject:[NSNumber numberWithInt:value]];

[(UILabel*)[(UITableViewCell *)sender.superview viewWithTag:456] setText:[NSString stringWithFormat:@"%@: %d", @"Stepper", value]];
}

关于ios - UIStepper 没有更新相应的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11067334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com