2016-08-21 52 views
0

我在cellForRowAtIndexPath方法中設置UILabel,當用戶改變文本時,標籤應該改變。但即使不是零,也不會改變。不能訪問UILabel

@property UILabel *myLabel; 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, textView.frame.size.width, 37)]; 
    self.myLabel.text = @"Old Text"; 
} 

- (void)textViewDidChange:(UITextView *)textView { 
    self.myLabel.textColor = [UIColor redColor]; 
    self.myLabel.text = @"New Text"; 
} 

我發現了一個解決方案,有些人在主線程中更改UILabel,但這對我不起作用。

dispatch_async(dispatch_get_main_queue(), ^{ 
     // Do in main thread 
}); 
+2

您發佈的代碼可能有效。你的'cellForRowAtIndexPath'不會出隊或創建新的單元格,也不會返回單元格。它不會將您創建的標籤附加到單元格。如果需要幫助,請發佈整個'cellForRowAtIndexPath'方法以及有關正在使用的'self.myLabel'屬性的信息。 –

+0

你沒有創建一個新的單元格。每次調用cellForRowAtIndexPath時,都會再次初始化您的標籤,並再次分配「舊文本」。 –

回答

0

如果要修改屏幕上的「對飛」細胞的標籤,那麼你需要一種方法來達到的標籤。

我通常做的是爲IB中的單元格創建一個模板,定義一個自定義子類UITableViewCell,並將該單元格的類設置爲該自定義子類。然後我在單元格和我創建的所有自定義字段之間連接插座。

在我的cellForRowAtIndexPath方法中,我將一個單元出隊並將其轉換爲我的自定義類型。然後,我通過單元格參考單元格中的字段,例如,

cell.myLabel

如果您在cellForRowAtIndexPath中向您的單元添加了一個新字段,則會出現一些問題。首先,您需要確保在回收單元格時不添加新字段的其他副本。其次,如果你想修改它,你需要一種方法來到現場,就像你的問題一樣。有辦法解決這些問題,但我上面概述的方法更清潔。

0

Duncan,對不起,這裏是cellForRowAtIndexPath的其餘部分。我認爲這不可能是重用單元格的問題,因爲我不滾動,因此不應該重新創建單元格。

@property NSString *textField_1; 
@property NSString *textField_2; 
// ... 
@property UILabel *myLabel; 

@end 

@implementation TableViewController 

- (void)textViewDidChange:(UITextView *)textView 
{ 
    self.myLabel.textColor = [UIColor redColor]; 
    self.myLabel.text = @"New Text"; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleTableCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 
    if(indexPath.section == 0) 
    { 
     float displayResolutionWidth = self.view.frame.size.width; 
     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, displayResolutionWidth - 165, 50)]; 
     textField.delegate = self; 
     textField.tag = indexPath.row; 
     textField.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

     UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, displayResolutionWidth - 90, 176)]; 
     textView.delegate = self; 
     textView.tag = indexPath.row; 
     textView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

     self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, textView.frame.size.width, 37)]; 
     self.myLabel.textColor = [UIColor redColor]; 

     if(indexPath.row == 17) 
     { 
      cell.accessoryView = textView; 
      [textView addSubview:self.myLabel]; 
     } 
     else if (indexPath.row != 17) 
     { 
      cell.accessoryView = textField; 
     } 
     if (self.object) 
     { 
      if (indexPath.row == 0) 
       textField.text = self.object.property_1; 
      if (indexPath.row == 1) 
       textField.text = self.object.property_2; 
     } 
     else if (!self.object) 
     { 
      if (indexPath.row == 0) 
       textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"Placeholder_1", @"") 
                        attributes:@{NSForegroundColorAttributeName: [UIColor greenColor]}]; 
      if (indexPath.row == 1) 
       textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"Placeholder_2", @"") 
                        attributes:@{NSForegroundColorAttributeName: [UIColor greenColor]}]; 
      if (indexPath.row == 17) 
       self.myLabel.text = "Old Text"; 
     } 
     if (indexPath.row != 17) 
     { 
      cell.textLabel.text = [self.arrayWithObjectData objectAtIndex:indexPath.row]; 
     } 
     else if (indexPath.row == 17) 
     { 
      UILabel *mylabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, 150, 40)]; 
      mylabel.text = self.arrayWithObjectData[17]; 
      mylabel.textColor = [UIColor COLOR_TEXT]; 
      [cell.contentView addSubview:mylabel]; 
     } 
    } 
     return cell; 
} 

@end