出于某种原因,即使有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] –