2013-03-11 78 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 ,但是當我向下滾動列表時,圖標在其他單元的accessoryView上隨機重新顯示(只有1個部分btw),並且滾動變得相當生澀和遲緩...我向上/向下滾動它顯示的實例越多! 爲什麼?這裏的錯誤在哪裏?

幫助,謝謝!

+0

因爲當你滾動單元已經重複使用其他行,我認爲你應該使用不同的CellReuseIdentifier 5行和9 – 2013-03-11 22:56:22

+0

我該怎麼做呢? – mindbomb 2013-03-11 23:05:30

回答

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; 
} 

剛剛完成,你也可以使用Eric的解決方案這樣的 - 它可能會更快,因爲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

好的,謝謝!我會在稍後檢查。但實際上我需要沒有圖像的單元顯示'披露'灰色直角圖標 – mindbomb 2013-03-12 12:17:54

+0

是5和9仍然會顯示圖像。其他人不是。去嘗試一下。 – calimarkus 2013-03-12 13:32:20

+0

並閱讀uitableview文檔 - 它解釋了重用。 – calimarkus 2013-03-12 13:32:47

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 2013-03-13 01:00:11