2017-08-30 36 views
0

出於某種原因,即使有10個不同的項目要顯示在我的數組中,我的tableView仍會多次顯示最近發佈的項目的數據。 E.g.而不是顯示在我的tableView單元格中的10個不同的項目,它只顯示最近10次在所有10個單元格中的後?任何想法,爲什麼這可能是?我嘗試使用兩個不同的單元格標識符來查看是否可以解決問題,但沒有骰子。見下面的代碼。iOS/Obj-C - tableView中的單元格多次顯示相同的數據?

ViewController.m

- (int)numberOfSectionsInTableView: (UITableView *)tableview 

{ 
    return 1; 

} 


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 


     if (neighbourDetail > 0) { 


      NSString *thisUserId = [neighbourDetail objectForKey:@"uid"]; 


      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"targetuser CONTAINS[cd] %@", 
            thisUserId]; 

      NSArray *resultArray = [self.reviewData filteredArrayUsingPredicate:predicate]; 


      return [resultArray count]; 

} else { 


    NSString *thisUserId = [self.myFriendData objectForKey:@"uid2"]; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"targetuser CONTAINS[cd] %@", 
           thisUserId]; 

    NSArray *resultArray = [self.reviewData filteredArrayUsingPredicate:predicate]; 

    return [resultArray count]; 


} 

} 

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


    if (neighbourDetail > 0) { 

     static NSString *DogTableIdentifier = @"ReviewTableViewCell"; 

     ReviewTableViewCell *cell = (ReviewTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DogTableIdentifier]; 
     if (cell == nil) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ReviewTableViewCell" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 

     } 
     NSString *thisUserId = [neighbourDetail objectForKey:@"uid"]; 


     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"targetuser CONTAINS[cd] %@", 
            thisUserId]; 

     NSArray *resultArray = [self.reviewData filteredArrayUsingPredicate:predicate]; 



     NSString *reviewDes = resultArray[0][@"body"]; 
     cell.reviewText.text = reviewDes; 


     NSString *firstName = resultArray[0][@"from first"]; 
     cell.firstText.text = firstName; 


     NSString *timeStamp = resultArray[0][@"published at"]; 
     cell.timeText.text = timeStamp; 

     NSString *secondLink = resultArray[0][@"from photo"]; 


     [cell.profilePic sd_setImageWithURL:[NSURL URLWithString:secondLink]]; 

     return cell; 


    } else if (neighbourDetail == NULL) { 

     static NSString *DogTableIdentifier2 = @"ReviewTableViewCell"; 

     ReviewTableViewCell *cellTwo = (ReviewTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DogTableIdentifier2]; 
     if (cellTwo == nil) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ReviewTableViewCell" owner:self options:nil]; 
      cellTwo = [nib objectAtIndex:0]; 

     } 

     NSString *thisUserId = [self.myFriendData objectForKey:@"uid2"]; 

     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"targetuser CONTAINS[cd] %@", 
            thisUserId]; 

     NSArray *resultArray = [self.reviewData filteredArrayUsingPredicate:predicate]; 


     NSString *reviewDes = resultArray[0][@"body"]; 
     cellTwo.reviewText.text = reviewDes; 

     NSString *firstName = resultArray[0][@"from first"]; 
     cellTwo.firstText.text = firstName; 


     NSString *timeStamp = resultArray[0][@"published at"]; 
     cellTwo.timeText.text = timeStamp; 

     NSString *secondLink = resultArray[0][@"from photo"]; 


     [cellTwo.profilePic sd_setImageWithURL:[NSURL URLWithString:secondLink]]; 


     return cellTwo; 
    } 

    return 0; 

    } 
+0

可能是Dublicate值包含在您的數組中。 –

+1

resultArray [0]應該是resultArray [indexPath.row] –

回答

3

你的問題是你是顯示在單元陣列的只有第一個元素像

NSString *reviewDes = resultArray[0][@"body"]; 

應該

NSString *reviewDes = resultArray[indexPath.row][@"body"]; 

而且最好是保持在cellForRow每次strong參考,而不是過濾器和numberOfRow

like

@property (nonatomic, strong) NSArray *resultArray; 

,當你的數據來自無論是從web服務或本地數據庫中爲它分配

它使你的有點快

希望這對您有所幫助

+0

你100%正確 - 謝謝你太多了哈哈。我會接受這個答案,一旦它讓我(說我需要等待5分鐘)。 – Brittany

+0

@Brittany歡迎....試試吧,讓我知道你是否仍然有問題。快樂編碼 –

0

在cellForRow方法,你犯了一個錯誤:

resultArray[0] --> resultArray[indexPath.row] 

你需要給你的當前indexPath的行你的,所以它會顯示爲每你的排。

結帳更多約indexPath

1

我相信這是因爲每你用相同的用戶ID過濾數據,結果顯示總是相同的結果。

如果結果數組將始終返回1,那麼它的確定,否則你必須使用的resultArray[indexPath.row]代替resultArray[0]

什麼是neighbourDetail包含?

0

在cellForRow方法中,您總是從resultArray [0]中填充數據。

resultArray[0] ----> resultArray[indexPath.row] 

表達式indexPath.row爲每行增加1,基本上是行號。

希望它有幫助。

0

使用這個簡單的代碼

NSArray *resultArray = [self.reviewData filteredArrayUsingPredicate:predicate]; 

NSDictionary*dict=[resultArray objectAtIndex:indexPath.row]; 
cellTwo.reviewText.text = [dict valueForKey:@"body"]; 
cellTwo.firstText.text = [dict valueForKey:@"from"]; 
cellTwo.timeText.text = [dict valueForKey:@"published at"]; 

NSString *secondLink = [dict valueForKey:@"from photo"]; 


[cellTwo.profilePic sd_setImageWithURL:[NSURL URLWithString:secondLink]]; 


return cellTwo; 
0

IndexPath在UITableView的委託和數據源的方法將幫助您精確識別您正在使用的這部分,並行。從蘋果文檔

func cellForRow(at indexPath:IndexPath) - > UITableViewCell?

參數IndexPath索引路徑在tableview中查找行。

所以當你在tableview上工作時,你可以使用indexpath來訪問特定的單元格。填充單元格或選擇tableView時使用indexPath不是硬編碼索引。 在你的情況你已經使用resultArray[0]你應該使用resultArray[indexPath.row]

相關問題