UIStepper不更新相应的标签 [英] UIStepper not updating the Corresponding Label

查看:88
本文介绍了UIStepper不更新相应的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UIStepper control that i created programmaticly in my UITableViewCell. I also created a UILabel programmaticly for the cell. whats happening is that the stepper when pressed is accessing a UILabel located in a different cell i.e index path 1 and not 0. here is the code.

- (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中创建的最后一个单元格处的标签. 因此,您不能以这种方式使用它.

stepperLabel will always point to the label at the last Cell you created in cellForRowAtIndexPath. So you can't use it this way.

编辑答案:

- (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]];
}

这篇关于UIStepper不更新相应的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆