200 개가 넘는 셀을 가진 UITable이 있습니다. 이 테이블은 네트워크에서 들어오는 데이터와 함께 아름답게 작동합니다. 이제 데이터와 일치하도록 레이블의 배경색을 변경하는 방법을 추가해야합니다 (값이 감소하면 빨간색, 값이 증가하면 녹색). 이것은 초기에는 잘 작동하는 것으로 보이지만 잠시 후에 값이 잘 갱신 되더라도 색상은 정적이됩니다. 내가 내 테이블을 표시하는 코드를 업데이트 한색상이 UITableViewCells에서 변경되지 않습니다.
업데이트 : 아래 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];
}
왜이 코드가 layoutSubviews에 있습니까? 그것은 매번 부르지 않을 것입니다. cellForRowAtIndexPath에 있어야합니다. – jrturton
아래의 답변을 읽은 후 코드를 적절한 위치로 옮겼습니다. 주의 해 주셔서 감사합니다. – Seb