如何在uilabel上添加uipickerview选定的行值?

问题描述:

我正在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

价值是如何申报的? –

这里是东西。 “价值”是全局变量,所以当你刷新你的表视图,该代码获取的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