2015-04-06 34 views
1

我有一個UITableViewCell包含兩個UIImageViewimgView1imgView2),基於我不得不隱藏imgView2數據源。它工作正常,期望一件事。在這些情況下,當我需要隱藏imgView2並且我有很多單元格imgView2將滾動到底部後顯示。我在需要時以編程方式隱藏imgView2,所以我不明白是什麼原因導致此問題。有沒有可能解決這個問題?或者我應該創建另一個自定義單元類來分隔兩個佈局?的UIImageView顯示在錯誤的UITableViewCell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object { 

    NSString *contentCreator = [NSString stringWithFormat:@"%@", object[@"contentCreator"]]; 

MDTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 

if ([contentCreator isEqualToString:[PFUser currentUser].objectId]) { 

    cell.usernameLabl.text = object[@"username"]; 

    PFFile *avatar1 = [Helper currentUserPhoto]; 
    cell.imageView2.layer.cornerRadius = cell.imageView2.frame.size.width/2; 
    cell.imageView2.clipsToBounds = YES; 
    cell.imageView2.layer.borderWidth = 1.0f; 
    cell.imageView2.layer.borderColor = [UIColor whiteColor].CGColor; 
    cell.imageView2.file = senderAvatar; 
    [cell.imageView2 loadInBackground]; 


    PFQuery *queryAvatar = [PFUser query]; 
    queryAvatar.cachePolicy = kPFCachePolicyCacheThenNetwork; 
    [queryAvatar whereKey:@"objectId" equalTo:object[@"recipientUser"]]; 
    [queryAvatar getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) { 

     if (object) { 
      PFFile *avatar2 = [object objectForKey:@"profilePhoto"]; 
      cell.imageView1.layer.cornerRadius = cellMessage.imageView1.frame.size.width/2; 
      cell.imageView1.clipsToBounds = YES; 
      cell.imageView1.file = senderAvatar; 

      [cell.imageView1 loadInBackground]; 
     } 
    }]; 
} 

else { 

    cell.imageView2.hidden = YES; 
    cell.usernameLabl.text = object[@"usernameSender"]; 

    PFQuery *queryAvatar2 = [PFUser query]; 
    queryAvatar2.cachePolicy = kPFCachePolicyCacheThenNetwork; 
    [queryAvatar2 whereKey:@"objectId" equalTo:object[@"senderUser"]]; 
    [queryAvatar2 getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) { 

     if (object) { 

      PFFile *senderAvatar = [object objectForKey:@"profilePhoto"]; 
      cell.imageView1.layer.cornerRadius = cell.imageView1.frame.size.width/2; 
      cell.imageView1.clipsToBounds = YES; 
      cell.imageView1.file = senderAvatar; 

      [cell.imageView1 loadInBackground]; 

     } 
    }]; 


} 
return cellMessage; 

} 
+0

從哪裏獲得contentCreator值? 你還沒有檢查objectAtIndexPath。現在當「contentCreator」會改變? – Samir 2015-04-06 12:43:16

+0

@Samir我更新了代碼。 – rihe 2015-04-06 12:46:47

+0

現在它很好,請嘗試按照ShahiM的建議取消隱藏imageView2。 如果有條件,您可以取消隱藏它。 – Samir 2015-04-06 12:52:51

回答

1

我認爲它是由dequeueReusableCellWithIdentifier:造成的故障。假設你隱藏imageView2的單元格x,並且向下滾動時,用於x的單元格將被重新用於另一個單元格'y',因此在單元格y中,由於單元格x的代碼,imageView2仍然隱藏。

所以最好的方法是在cellForRowAtIndexPath開頭取消隱藏imageView2

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object 
{ 

    MDTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 

    cell.imageView2.hidden = NO; 

    //rest of your code... 
+0

看來它工作正常,ShahiM非常感謝你。還有一個問題,你認爲創建另一個單元類並處理兩種情況會更好嗎? – rihe 2015-04-06 13:21:35

+0

不同的細胞亞類是正確的方法。但如果你在細胞之間只有一些細微的變化,那麼它不值得麻煩,你的方法就沒有問題。 – ShahiM 2015-04-07 06:02:33

2

什麼是你使用dequeueReusableCell的完整代碼,如果在出列分配它時爲零,那麼它是否爲incase?

可能發生的情況是,當單元格對於第一個/可見表格框架的單元格更多時,dequeueReusableCellWithIdentifier:返回nil,並且單元格將顯示imageView2並在滾動時出現,如果出列單元格被重用,則會顯示imageView2。

+0

就是這樣,我沒有處理,當它是零。我應該怎麼做? – rihe 2015-04-06 12:37:48

+1

dequeueReusableCellWithIdentifier:forIndexPath:總是返回非零單元格 – oxigen 2015-04-06 12:38:22

相關問題