2012-03-25 4 views
1

私は各セルがいくつかのセクションに分割されており、それぞれのセクションで異なるラベルを持つUITableViewを持っています。テーブルは、セルのラベルに値を設定するすべてのデータを含むNSArrayのNSArrayによって作成されます。 UITableViewのこの部分はうまく動作します。iOS:UITableViewCellのcontentViewでカスタムラベルに正しくアクセスするにはどうすればいいですか?

NSDictionariesの1つで値の一部を変更し、更新されたNSArrayでテーブルをリロードすると問題が発生します。通常、私が[myTableView reloadData];を呼び出すと、(デバッグによって)何も更新されず、更新されたデータが処理されていることがわかります。しかし、私が標準を変更する場合:if (cell == nil) {if (1) {に、cellForRowAtIndexPathメソッドでは、それは美しく動作します。なぜif(1) {が動作するのか理解していますが、セルを再利用できず、ラベルテキストを変更できない理由がわかりません。

なぜif (cell == nil)が機能しないのですか?それは、各セルを再初期化するための巨大なリソースドレインですか?

CODE:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *cellIdentifier = @"myCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier]; 

if (/*cell == nil*/1) { 
    // Initialize Custom Cell 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

    //// Background View 
    cellBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 622, 43)]; 
    [cellBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Images/Library/phase_table_cell.png"]]]; 

    //// Name Label 
    nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 18)]; 
    [nameLabel setBackgroundColor:[UIColor clearColor]]; 
    [cellBackgroundView addSubview:nameLabel]; 

    //// Percent Complete Label 
    percentCompleteLabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 10, 30, 18)]; 
    [percentCompleteLabel setBackgroundColor:[UIColor clearColor]]; 
    [percentCompleteLabel setTextAlignment:UITextAlignmentCenter]; 
    [cellBackgroundView addSubview:percentCompleteLabel]; 

    //// Overall Status Label 
    overallStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(352, 7, 63, 30)]; 
    [overallStatusLabel setBackgroundColor:[UIColor clearColor]]; 
    [overallStatusLabel setFont:[UIFont boldSystemFontOfSize:12.0]]; 
    [overallStatusLabel setLineBreakMode:UILineBreakModeWordWrap]; 
    [overallStatusLabel setNumberOfLines:2]; 
    [overallStatusLabel setTextAlignment:UITextAlignmentCenter]; 
    [cellBackgroundView addSubview:overallStatusLabel]; 

    //// Finish Date Label 
    finishDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(425, 10, 55, 18)]; 
    [finishDateLabel setBackgroundColor:[UIColor clearColor]]; 
    [finishDateLabel setTextAlignment:UITextAlignmentCenter]; 
    [finishDateLabel setFont:[UIFont systemFontOfSize:12.0]]; 
    [cellBackgroundView addSubview:finishDateLabel]; 

    //// Overall Weight Label 
    overallWeightLabel = [[UILabel alloc] initWithFrame:CGRectMake(505, 10, 30, 18)]; 
    [overallWeightLabel setBackgroundColor:[UIColor clearColor]]; 
    [overallWeightLabel setTextAlignment:UITextAlignmentCenter]; 
    [cellBackgroundView addSubview:overallWeightLabel]; 

    //// Green Risk View 
    greenRiskView = [[UIView alloc] initWithFrame:CGRectMake(557, 4, 61, 10)]; 
    [greenRiskView setBackgroundColor:[UIColor greenColor]]; 
    [greenRiskView setHidden:YES]; 
    [cellBackgroundView addSubview:greenRiskView]; 

    //// Yellow Risk View 
    yellowRiskView = [[UIView alloc] initWithFrame:CGRectMake(557, 17, 61, 10)]; 
    [yellowRiskView setBackgroundColor:[UIColor yellowColor]]; 
    [yellowRiskView setHidden:YES]; 
    [cellBackgroundView addSubview:yellowRiskView]; 

    //// Red Risk View 
    redRiskView = [[UIView alloc] initWithFrame:CGRectMake(557, 30, 61, 10)]; 
    [redRiskView setBackgroundColor:[UIColor redColor]]; 
    [redRiskView setHidden:YES]; 
    [cellBackgroundView addSubview:redRiskView]; 

    [cell.contentView addSubview:cellBackgroundView]; 
} 

// Get Current Dictionary 
NSDictionary *dictForIndexPath = [self.phaseArray objectAtIndex:[indexPath row]]; 
// Set Elements 
[nameLabel setText:[dictForIndexPath objectForKey:@"name"]]; 
[percentCompleteLabel setText:[dictForIndexPath objectForKey:@"percentComplete"]]; 
[overallStatusLabel setText:[dictForIndexPath objectForKey:@"overallStatus"]]; 
[overallWeightLabel setText:[[NSNumber numberWithInt:[[dictForIndexPath objectForKey:@"overallWeight"] intValue]] stringValue]]; 
//// Create Finish Date String 
NSString *finishDateString = [NSString stringWithFormat:@"%@/%@/%@", [dictForIndexPath objectForKey:@"finishDay"], [dictForIndexPath objectForKey:@"finishMonth"], [dictForIndexPath objectForKey:@"finishYear"]]; 
[finishDateLabel setText:finishDateString]; 
//// Pick Risk View 
NSString *riskColor = [dictForIndexPath objectForKey:@"riskColor"]; 
if ([riskColor isEqualToString:@"Green"]) { 
    [greenRiskView setHidden:NO]; 
    [yellowRiskView setHidden:YES]; 
    [redRiskView setHidden:YES]; 
} else if ([riskColor isEqualToString:@"Yellow"]) { 
    [greenRiskView setHidden:YES]; 
    [yellowRiskView setHidden:NO]; 
    [redRiskView setHidden:YES]; 
} else { 
    [greenRiskView setHidden:YES]; 
    [yellowRiskView setHidden:YES]; 
    [redRiskView setHidden:NO]; 
} 

return cell; 
} 
+0

あなたは本当にする必要がありますかテーブル全体をリロードしますか? 'UITableView'の' reloadRowsAtIndexPaths:withRowAnimation: 'でリロードできますか?あなたがそのルートを行くなら、私は独自のメソッドにセルの初期化を移します。例えば ' - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath'です。 – Gobot

答えて

3

は、おそらくあなたがif(cell==nil)ブロック内で値を代入していますか?そのブロック内では初期化のみが行われます。それから残りの部分を動かしてください。

//編集を(あなたはより多くの助けが必要な場合は、あなたの完全なcellForRowコードを投稿):あなたのコードを掲載した後に、今、私はあなたの問題を参照してください。

あなたはメンバ変数にすべてのラベルとビューを格納しています。..もちろん、if(cell != nil)ブロックが実行された場合にのみ発生します。その後、常に同じ単一セル(最後に割り当てられたセル)にアクセスします。したがって、少なくとも1つのセルを更新しています;

問題を解決するには、次のようにします。タグを使用して、対応するビューをセルから戻します。私はあなたのbackgroundViewのためにそれが表示されますが、あなたはあなたのビューのすべてのためにそれをしなければならない(INSTEADメンバ変数の。それらを削除します。)

static NSString *cellIdentifier = @"myCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier]; 

if (cell == nil) { 
    //// Background View 
    UIView* cellBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 622, 43)]; 
    cellBackgroundView.tag = 1; 
    [cellBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Images/Library/phase_table_cell.png"]]]; 
} 

// get the backgroundview from the current cell 
UIView* backgroundView = [cell.contentView viewWithTag: 1]; 

など..

+0

私はそれが事実だったと思う...あなたが見ることができるようにコードを掲載しました、うまくいけば助けになるでしょう。 –

+0

あなたの問題があります。私は自分の答えを更新しました。 – calimarkus

+0

ありがとうございました。 –

関連する問題