出於某種原因,即使有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;
}
可能是Dublicate值包含在您的數組中。 –
resultArray [0]應該是resultArray [indexPath.row] –