2016-08-21 7 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

ダンカン、申し訳ありませんが、残りは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 
関連する問題