2011-02-24 22 views
0

我是半新iOS開發人員。 當我嘗試自定義tableview單元格以將圖片置於說明文字下時,它會起作用,直到我滾動,然後它會全部搞砸。iOS 4.2:Tableview中的子視角表現怪異

當我向下滾動,然後備份,圖像是它不應該在一個小區:/

我刪除了代碼,因爲它不工作。但是我正在做的是創建一個帶有框架的UIView,裏面有一個UIImageView。然後我將它添加到cell.contentView。

其中一期工程,直到我滾動。 所以可能有人請幫助:/

編輯:這裏是什麼樣子的人感興趣,我得到它的工作。 (感謝喬納森)

#define LEVEL_TAG 1 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *CellIdentifier = @"Cell"; 
     UIImageView *showLevel; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 

      showLevel = [[[UIImageView alloc] initWithFrame: CGRectMake(65, 50, 0, 11)] autorelease]; 
      showLevel.tag = LEVEL_TAG; 
      [cell.contentView addSubview: showLevel]; 

     } else { 
      showLevel = (UIImageView *)[cell.contentView viewWithTag:LEVEL_TAG]; 
     } 
     NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 

     NSArray *levelArray = [dictionary objectForKey:@"Levels"]; 
     NSString *levelValue = [levelArray objectAtIndex:indexPath.row];  

     UIImage *levelImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource: levelValue ofType:@"png"]]; 
     CGImageRef starImage = levelImage.CGImage; 
     NSUInteger levelWidth = CGImageGetWidth(starImage); 
     showLevel.frame = CGRectMake(65, 50, levelWidth, 11); 
     showLevel.image = levelImage; 

     return cell; 
} 
+0

有沒有機會看到你的代碼?很可能你的cellForRowAtIndexPath方法有錯誤 – Vladimir 2011-02-24 19:57:27

回答

0

這是因爲路的tableView重用原來的細胞,而不是創造新的每次用戶滾動,這將讓一切慢,效率較低的時間。

查看關於定製UITableViewCells的Apple文檔中的Listing 5-3,它向您展示瞭如何在實際創建單元格的if語句內設置單元格,而不是單元格可能被重用的位置。

+0

哇,非常感謝!幫助如此之多。將顯示我做過以上任何人需要知道如何。 (: – 2011-02-24 20:39:16