2017-08-30 35 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]

相关问题