2015-12-21 61 views
-1

我正在uipickeruitableview's prototype cell我在那裏創建一個uilabel。現在我在標籤上添加選擇器視圖行選擇的值,但是我的問題是,當我從選擇器視圖選擇行時,所有標籤都會更新。如何在uilabel上添加uipickerview選定的行值?

值應在uilabel的選定行上更新。這是我的代碼和屏幕截圖。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    return [myData count]; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 

    UILabel *lable = (UILabel*)[cell viewWithTag:111]; 
    lable.tag = indexPath.row; 
    lable.text = value; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    _nameLabel.text =nil; 

    [self.mytableview reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:NO]; 

    [self bringUpPickerViewWithRow:indexPath]; 
} 



- (void)bringUpPickerViewWithRow:(NSIndexPath*)indexPath 
{ 
    UITableViewCell *currentCellSelected = [self.mytableview cellForRowAtIndexPath:indexPath]; 
    [UIView animateWithDuration:1.0f 
          delay:0.0f 
         options:UIViewAnimationOptionCurveEaseInOut 
        animations:^ 
    { 
     self.pickerView.hidden = NO; 
     self.pickerView.center = (CGPoint) { currentCellSelected.frame.size.width/2, self.mytableview.frame.origin.y + currentCellSelected.frame.size.height*4}; 


     self.mytableview.separatorStyle = UITableViewCellSeparatorStyleNone; 
     [self.mytableview setNeedsDisplay]; 
    } 
        completion:nil]; 


} 

- (void)hidePickerView 
{ 
    [UIView animateWithDuration:1.0f 
          delay:0.0f 
         options:UIViewAnimationOptionCurveEaseInOut 
        animations:^ 
    { 
     self.pickerView.center = (CGPoint){160, 800}; 
    } 
        completion:^(BOOL finished) 
    { 
     self.pickerView.hidden = YES; 
     [self.mytableview reloadData]; 
    }]; 
} 


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    return _countingarray.count; 
} 



- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component 
{ 

    [self.mytableview reloadData]; 
    [self hidePickerView]; 
    NSLog(@"row selected:%ld", (long)row); 
    NSString *resultString = _countingarray[row]; 

    value = resultString; 
} 


- (NSString*) pickerView:(UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent:(NSInteger)component 
{ 
    //return [NSString stringWithFormat:@"%d", row+1]; 
    return _countingarray[row]; 
} 

- (IBAction)editbuttonclicked:(id)sender { 

    if([self.mytableview isEditing] == NO){ 

     //[self.mytableview settitle:@"Done"]; 
     //set the table to editing mode 
     [self.mytableview setEditing:YES animated:YES]; 
    }else 

    { 

     //we are currently in editing mode 
     //change the button text back to Edit 
     //[self.editbutton setTitle:@"Edit"]; 
     //take the table out of edit mode 
     [self.mytableview setEditing:NO animated:YES]; 
    } 
} 

-(void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     [myData removeObjectAtIndex:indexPath.row]; 
     [self.mytableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 
} 

this is my screen shot when i select 2 from picker view at first cell than all the uilabel updates same value

+0

有人幫我PLZ –

+0

你已經有一個'myData'您使用爲了確定在tableView中應該顯示多少行,您應該可以使用該數組中的任何內容來確定每個標籤顯示的文本。在你做完這些之後,你應該需要'value',但是應該只處理該數組中的數據,然後再處理'reloadData'。 – luk2302

+0

價值是如何申報的? –

回答

0

這裏是東西。 「價值」是全局變量,所以當你刷新你的表視圖,該代碼獲取的tableView呼籲每一個細胞

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 

UILabel *lable = (UILabel*)[cell viewWithTag:111]; 
lable.tag = indexPath.row; 
lable.text = value; 
return cell; 
} 

現在注意

label.text= value 

對於每一個細胞的標籤獲得相同的值(無論它是)。

@ luk2302是正確的。在你的myData中管理這個。

建議的解決方案 可能有很多方法可以做到這一點。這是我可以立即建議的

沒有價值作爲全局變量。 添加NSIndexPath * currentCellPath作爲全局變量。

現在你didSelectRow在選擇器的didselectRow做到這一點

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath 
    { 
    currentCellPath = indexPath; 
    // remaining code goes here 
    _nameLabel.text =nil; 

    [self.mytableview reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:NO]; 

    [self bringUpPickerViewWithRow:indexPath]; 
    } 

現在做這個

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component 
{ 
UITableViewCell *cell = [self.myTableView cellForIndexPath:currentCellPath]; 
cell.label.text = _countingarray[row]; 
    [self.mytableview reloadData]; 
    [self hidePickerView]; 
    NSLog(@"row selected:%ld", (long)row); 
    //NSString *resultString = _countingarray[row]; // remove this 

    //value = resultString; //romove this 

}

+0

它給了我這樣的tableview和標籤的錯誤: - 「沒有可見的接口爲uitableview聲明選擇器cellforindexpath」。和另一個表錯誤; - 「在類型爲」uitableviewcell「的對象上找不到屬性標籤」「。 –

+0

我的不好,先用'[self。myTableView cellForRowAtIndexPath:currentCellPath]' – ibnetariq

+0

請告訴我爲什麼你必須爲你分配標籤標籤(111)並將其更改爲indexPath。 – ibnetariq

相關問題