2013-03-11 9 views
10

私はこのコードを使用して、私のUITableViewCellsの特定の行の「南京錠」アイコンを表示しようとしています:のUITableView細胞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しかし、私がリストを上下にスクロールすると、アイコンが他のセルのアクセサリビューでもランダムに再表示されます(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; 
} 

だけで完了するために、あなたもこのようエリックのソリューションを使用することができます - ImageViewのが毎回作成されていないので、それは、より速くかもしれません。しかし、それは最小の違いだけです。おそらくあなたの遅れには他の理由があります。あなたがスクロールすると、細胞が他の行のために再利用されているため

- (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

OK、ありがとう!私は後でそれを確認します。実際には、「開示」グレーの直角アイコンを表示する画像がないセルが必要です – 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