2015-10-17 71 views
0

我创建了一个应用程序,我使用“for循环”创建视图列表,并且每个视图都包含一些标签和pickerViews。当我点击并从pickerView中选择任何值时,我的uilabel会更新,但当我点击任何其他pickerview并从中选择不同的值时,它会更新到以前的每个标签。所以通过这种方式,每个UILabel中只显示最后一个选择器值。通过从选取器填充UIlabel上的数据查看

我知道它发生的原因,因为我创建了UILabel作为属性并通过“self”访问。但是,如果我不以这种方式访问​​,那么如何更新该标签的值。

示例代码:

for (NSMutableDictionary *itemDict in self.returnItemsArray) { 
    self.issueLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(18, shipmentY+3, 270, 20)]; 
      [_issueLabel setNumberOfLines:0]; 
      [_issueLabel setTextColor:LR_darkgray_color]; 
      [_issueLabel setFont:[UIFont systemFontOfSize:15]]; 
      [_issueLabel setTextAlignment:NSTextAlignmentLeft]; 
      [_issueLabel setTag:index]; 
      [_issueLabel setText:@"Size Change"]; 
      [returnOrderInfoView addSubview:_issueLabel]; 
} 

-(void)picker:(LRPickerView *)picker closedWithSelectedIndex:(NSInteger)index{ 

    if (picker.type == SIZE_PICKER){ 
    [self.issueLabel setText:[[_sizeDictionary objectForKey:selectedVar]uppercaseString]]; 
} 
} 

让我知道你需要的任何其他信息。

+0

你说:“当我点击并选择任意值从的UILabel获取并更新:

然后在picker:closedWithSelectedIndex:,该标签可以从阵列中使用类似检索更新,但当我点击任何其他pickerview并从中选择不同的值时,它会更新到每个以前的标签。“这绝对没有任何意义。我甚至会称之为“[**词沙拉**](https://en.wikipedia.org/wiki/Word_salad)”。 –

回答

0

从您所描述的内容来看,您的所有标签都有相同的参考。为了解决这个问题,你需要一种维护单独标签引用的方法,比如C中的指针。我建议有一个标签数组,而不是只使用self.issueLabel。一个属性将需要添加到您的类如:

@property NSMutableArray *issueLabels; 

你已经初始化您的标签后,它就像添加到阵列中让你的for循环:

[self.issueLabels addObject:issueLabel]; 

在各项指标然后issueLabels数组应该对应于self.returnItemsArray中的索引。

TTTAttributedLabel *issueLabel = ((TTTAttributedLabel *)self.issueLabels[index]); 

使用

[issueLabel setText:[[_sizeDictionary objectForKey:selectedVar]uppercaseString]];