200 以上のセルを持つ UITable があります。このテーブルは、ネットワークから入ってくるデータをうまく処理します。ここで、データに合わせてラベルの背景色を変更する方法を追加する必要があります (値が減少した場合は赤、値が増加した場合は緑)。これは最初はうまくいくように見えますが、しばらくすると、値が正常に更新されても色が静的になります。以下は、layoutSubviews メソッドにある私のコードの例です。
アップデート
テーブルを表示するようにコードを更新しました。データは完全に問題なくセルに割り当てられることに注意してください。値が何であれ、数分後に変更を拒否するのはセルの色です。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel* label = [[UILabel alloc] initWithFrame:self.contentView.bounds];
label.tag = 1;
[cell.contentView addSubview:label];
[label release];
}
UILabel *label = (UILabel*)[cell viewForTag:1];
float value = [label.text floatValue];
float newValue = [dataSource objectAtIndex:indexPath.row];
// Get the current value of the cell and compare it with the new value
if(value < newVal)
{
label.backgroundColor = [UIColor greenColor];
}
else if(value > newVal)
{
label.backgroundColor = [UIColor redColor];
}
label.text = [NSString stringWithFormat:@"%d", newValue];
}