2012-06-16 28 views
1

我有一個UIStepper控制,我編程創建在我的UITableViewCell。我還爲該單元編程創建了一個UILabel。最近發生的事情是,步進器按下時正在訪問位於不同單元中的UILabel,即索引路徑1而不是0.這裏是代碼。UIStepper不更新相應的標籤

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

行動嚴重需要一些幫助球員

+0

那麼發生了什麼?是'stepperPressed:'執行? 'sender.tag == row'是否真的如此?你記錄什麼會告訴你? –

+0

步進器按下正在執行,但條件語句不正確工作它有時工作。 – KING

回答

5

stepperLabel總是指向標籤在你的cellForRowAtIndexPath創建的最後一個單元格。 所以你不能這樣使用它。

編輯答案:

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

我用你的例子,但我的日誌ouptut是相同的步進是在第0行 stepperLabel在第1行 – KING

+0

你是對的,請檢查我編輯的答案... – xapslock

+0

Xapslock謝謝你我的朋友。我一直試圖找出這個問題好幾天了。 Iv只進行了大約9個月的編程,並且在Objective-C中進行了不到一個月的時間。謝謝你的幫助。 – KING