2013-03-11 4 views
10
이 코드 사용하여 내 UITableViewCells의 특정 행에 '자물쇠'아이콘을 표시하기 위해 노력하고

:jQuery과 셀 accessoryView 이미지 문제

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"]; 

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row]; 
    cell.textLabel.text= topic.name; 

    if ((indexPath.row == 5) || (indexPath.row == 9)) 
    { 
      cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];; 
      [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)]; 

    } 

    return cell; 
} 

나는 재미있는 결과를 얻고을 - 자물쇠는 처음 행 5에 표시됩니다 , 9하지만 목록을 아래로 스크롤하면 다른 셀의 accessoryView에서도 아이콘이 무작위로 다시 표시됩니다 (단 1 섹션 btw 만 있음). 스크롤이 꽤 번쩍 거리고 뒤죽박죽이됩니다. 위아래로 스크롤하면됩니다. 더 많은 인스턴스가 표시됩니다! 왜? 여기에 버그가 어디 있니?

도움, 감사합니다!

+0

, 난 당신이 내가 그렇게 어떻게 행 5, 9 –

+0

에 대해 다른 CellReuseIdentifier를 사용한다고 생각? – mindbomb

답변

21

세포가 재사용됩니다. 매번 accessoryView를 재설정해야합니다. 단지 작은 변화 그것은 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"]; 

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row]; 
    cell.textLabel.text= topic.name; 

    if ((indexPath.row == 5) || (indexPath.row == 9)) 
    { 
     cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]]; 
     [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)]; 
    } else { 
     cell.accessoryView = nil; 
    } 

    return cell; 
} 

그냥 완료, 당신은 또한 다음과 같은 에릭의 솔루션을 사용할 수 있습니다 - 이미지 뷰가 때마다 생성되지 않기 때문에 그것이 더 빠를 수 있습니다. 하지만 그 차이는 단지 최소한입니다. 아마 당신의 시차는 다른 이유가있을 것입니다. 스크롤 할 때 셀이 다른 행에 재사용되어 있기 때문에

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = nil; 
    if(indexPath.row == 5 || indexPath.row == 9) { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCellWithImage"]; 
     cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];; 
     [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)]; 
    } else { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"]; 
    } 

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row]; 
    cell.textLabel.text= topic.name; 

    return cell; 
} 
+0

좋습니다, 감사합니다! 나는 나중에 그것을 점검 할 것이다. 실제로 '공개'회색 직각 아이콘을 표시하는 이미지가없는 셀이 필요합니다 – mindbomb

+0

예 5와 9는 여전히 이미지를 표시합니다. 그렇지 않은 사람. 그냥 시도 해 봐. – calimarkus

+0

을 읽고 uitableview 설명서를 읽으십시오 - 재사용을 설명합니다. – calimarkus

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell; 
    if(indexPath.row == 5 || indexPath.row == 9) { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCellWithImage"]; 
    } else { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"]; 
    } 

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row]; 
    cell.textLabel.text= topic.name; 

    if ((indexPath.row == 5) || (indexPath.row == 9)) 
    { 
     cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];; 
     [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)]; 

    } 

    return cell; 
} 
+0

죄송합니다, 정확하지 않습니다, jaydee3의 솔루션은 훨씬 간단합니다 .. 감사합니다! – mindbomb