2013-09-30 4 views
2

iOS7로 업그레이드 한 후 나타나는 문제로, 특정 tableview 셀의 배경색을 변경하려고 할 때 올바른 셀을 색칠하지 못합니다 (일반적으로 다른 것들과 함께 지정된 것들). 아래 코드에서 볼 수 있듯이 강조 표시 할 유형을 정의한 다음 색상을 변경합니다. iOS 업그레이드 이전에 완벽하게 작동했기 때문에 어떤 변화가 발생했는지 정확히 알지 못했습니다.iOS7에서 tableview 셀의 배경색 변경

빠른 편집 : 또한 테이블 뷰를 아래로 스크롤하여 백업하면 더 많은 셀을 색칠합니다. tableview 컨트롤러가 처음으로로드 될 때 색이 표시됩니다 (전혀 도움이되는 경우).

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString* type=[[self.HandPointer.player_hand objectAtIndex:indexPath.row]cardType]; 
    if ([type isEqualToString:@"ace"]){ 
     cell.backgroundColor = [UIColor colorWithRed:0.81 green:0.91 blue:0.81 alpha:1.0]; 
    } 
} 

답변

4

내가 tableView: cellForRowAtIndexPath: 방법으로 세포의 정의를하고 생각이 더 좋다. 이 방법에서는

if ([type isEqualToString:@"ace"]) 
{ 
    cell.backgroundColor = [UIColor aceColor]; 
} 
else // this else is important. If you add this, scrolling works fine. 
{ 
    cell.backgroundColor = [UIColor otherCellColor]; 
} 
+1

예! "else"가 문제를 해결하도록했습니다. 그러나 tableView : cellForRowAtIndexPath 메소드가 더 좋은 이유는 무엇입니까? –

+0

물론 이것은 일반적인 규칙은 아니지만 일반적으로이 방법은 셀 사용자 정의에 사용됩니다. – caglar

+0

커스텀 화가 willDisplayCell에있는 경우 문제를 일으키는 iOS 프레임 워크에 대해 이해하고 싶습니다. –

0

재사용 가능한 단일 셀 스타일이있을 가능성이 높습니다. 에이스와 다른 모든 셀 스타일에 재사용 가능한 셀 스타일을 고려하십시오. willDisplayCell이 아니라 cellForRowAtIndexPath에 배경색을 설정하십시오.

의사 코드 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
. 
. 
. 
NSString* type=[[self.HandPointer.player_hand objectAtIndex:indexPath.row]cardType]; 
if ([type isEqualToString:@"ace"]){ 
{ 
    // load a cell with the background color desired 
    cell = 
    cell.backgroundColor = 
    . 
    . 
    . 
    return (cell); 
} 

// else a normal cell 
cell = 
. 
. 
. 
} 
관련 문제