2013-02-26 2 views
0

코어 데이터와 NSFetchedResultsController을 통해 일부 객체를 가져 왔으며 부울 속성 중 하나를 기반으로 조건부 서식을 적용하려고합니다. Liked으로 표시된 경우 텍스트 색상을 파란색으로 지정합니다.콘텐츠에 따라 UITableViewCell에 조건부 서식을 적용하는 올바른 방법은 무엇입니까?

내가 발견 한 문제는 Liked 인 것보다 테이블을 스크롤 할 때 YES이 색칠되고 있다는 것입니다. 규칙적인 패턴이기도합니다. 예를 들어 아래로 스크롤 할 때마다 여섯 번째 항목이 있습니다. 셀 큐잉 및 재사용과 관련이 있다고 생각하지만, 나는 무엇을 모릅니다. 당신은 항상 할 수있는 모든 여섯 번째 셀

if ([[thisQuote isLiked] boolValue]) { 
    cell.textLabel.textColor = [UIColor blueColor]; 
} 
else cell.textLabel.textColor = [UIColor blackColor]; 

답변

2

, 당신은 모든 세포에 대한 속성의 텍스트 색상을 재설정해야 : 여기 내 코드의

if(indexPath.row % 6 == 0) { 
    // Set blue color 
} 
else { 
    // Set black color 
} 

또는 쉽게 :

cell.textLabel.textColor = (indexPath.row % 6 == 0) ? [UIColor blueColor] : [UIColor blackColor]; 
+0

그 엄마 전혀 차이가 없다. – Luke

+0

스크래치 (scool)는 boolValue가 아닌 NSNumber에 액세스하고있었습니다. 고마워요 :) – Luke

+0

당신은 환영합니다 :) – Bigood

0

: 당신이 dequeueReusableCellWithIdentifier:를 사용하는 것처럼

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath: indexPath]; 

    Quote *thisQuote = [self.fetchedResultsController objectAtIndexPath: indexPath]; 

    cell.textLabel.numberOfLines = 4; 
    cell.textLabel.font = [UIFont boldSystemFontOfSize: 12]; 
    cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    cell.textLabel.text = [[self.fetchedResultsController objectAtIndexPath: indexPath] quote]; 

    if ([[thisQuote isLiked] boolValue]) { 
     cell.textLabel.textColor = [UIColor blueColor]; 
    } 

    return cell; 
} 
+0

나는 여섯 번째 세포가 모두 생기길 원하지 않는다. 지금은 그런 일이 일어나고있다. 나는 bool이있는 사람 만 색칠하기를 원한다. – Luke

관련 문제