Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在uilabel上添加uipickerview选定行值?_Ios - Fatal编程技术网

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

Ios 如何在uilabel上添加uipickerview选定行值?,ios,Ios,我正在uipicker内部uitableview的原型单元中工作,在那里我创建了一个uilabel。现在,我在标签上添加了选择器视图行选定值,但我的问题是,当我从选择器视图中选择行时,所有标签都会更新 应更新uilabel上选定行上的值。这是我的代码和屏幕截图 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [myData count];

我正在
uipicker
内部
uitableview的原型单元
中工作,在那里我创建了一个
uilabel
。现在,我在标签上添加了选择器视图行选定值,但我的问题是,当我从选择器视图中选择行时,所有标签都会更新

应更新
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];
    }
}
事情是这样的。“value”是全局变量,所以当您重新加载表视图时,会为表视图中的每个单元格调用此代码

- (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作为全局变量

现在你做了这件事

     -(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
现在在你的拣选者的行列里做这个

     -(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

}

您已经有了一个
myData
,您可以使用它来确定tableView中应该显示多少行,您可能应该使用该数组中的任何内容来确定每个标签显示的文本。完成此操作后,您应该不再需要
,但应该只操作该数组中的数据,然后
重新加载数据
。该值是如何声明的?@luk2302您能告诉我如何使用myData添加值。就像您在文章末尾从数组中删除一些对象一样,您可以在数组中替换或添加对象。感谢您的回复@mihirmehta NSString*值;这就是我声明value.its的方式,它给了我tableview的错误,标签是这样的:-“uitableview没有可见接口声明选择器cellforindexpath”。表的另一个错误;-在类型为“uitableviewcell”的对象上找不到属性标签。我的问题是,首先使用“[self.myTableView cellForRowAtIndexPath:currentCellPath]”,请告诉我为什么必须为标签(111)分配标记并将其更改为indexPath。因为我将在myTableView中创建111 tableview单元格。