在我的tableView中,我试图为每个字典显示某个类型的单元格,其中swapvalue等于0.这就是说,由于某种原因,下面的代码只会抓取第一个字典的值在我的数组中( AKA: )第一个字典中swapvalue的值为0,因此,我的表显示所有单元格,就好像后面的数组也有swapvalue 0 - 即使他们没有)。我怎样才能改变这一点,以便每个单元格从相应的字典中获取swapvalue?我怀疑它与indexPath.row有关,但我不确定如何纠正这个问题?
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self.messageDataFriends count] > 0 ) {
NSDictionary *userDictInfo = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"diosSession"]];
DIOSSession *session = [DIOSSession sharedSession];
[session setUser:userDictInfo];
[session user];
NSString *targetedUser = [self.messageDataFriends objectForKey:@"uid2"];
NSString *myID = [session user][@"user"][@"uid"];
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"uid contains[cd] %@", targetedUser];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"targetuser contains[cd] %@", myID];
NSPredicate *p3 = [NSPredicate predicateWithFormat:@"uid contains[cd] %@", myID];
NSPredicate *p4 = [NSPredicate predicateWithFormat:@"targetuser contains[cd] %@", targetedUser];
NSCompoundPredicate *pIntermediary1 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p2,]];
NSCompoundPredicate * pIntermediary2 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p3, p4,]];
NSCompoundPredicate *pFinal = [NSCompoundPredicate orPredicateWithSubpredicates:@[pIntermediary1, pIntermediary2]];
NSArray *filtered = [self.messages filteredArrayUsingPredicate:pFinal];
return [filtered count];
}
else {
NSDictionary *userDictInfo = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"diosSession"]];
DIOSSession *session = [DIOSSession sharedSession];
[session setUser:userDictInfo];
[session user];
NSString *targetedUser = [self.messageData objectForKey:@"uid"];
NSString *myID = [session user][@"user"][@"uid"];
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"uid contains[cd] %@", targetedUser];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"targetuser contains[cd] %@", myID];
NSPredicate *p3 = [NSPredicate predicateWithFormat:@"uid contains[cd] %@", myID];
NSPredicate *p4 = [NSPredicate predicateWithFormat:@"targetuser contains[cd] %@", targetedUser];
NSCompoundPredicate *pIntermediary1 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p2,]];
NSCompoundPredicate * pIntermediary2 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p3, p4,]];
NSCompoundPredicate *pFinal = [NSCompoundPredicate orPredicateWithSubpredicates:@[pIntermediary1, pIntermediary2]];
NSArray *filtered = [self.messages filteredArrayUsingPredicate:pFinal];
return [filtered count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *data = self.messages[indexPath.section];
id swapvalue = data[@"swapvalue"];
NSLog(@"SWAP VALUE %@", swapvalue);
if ([neighbours count] > 0 && [swapvalue isEqualToString: @"0"]) {
static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";
SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2 forIndexPath:indexPath];
NSString *targetedUser = [self.messageData objectForKey:@"uid"];
NSDictionary *userDictInfo = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"diosSession"]];
DIOSSession *session = [DIOSSession sharedSession];
[session setUser:userDictInfo];
[session user];
NSString *myID = [session user][@"user"][@"uid"];
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"uid contains[cd] %@", targetedUser];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"targetuser contains[cd] %@", myID];
NSPredicate *p3 = [NSPredicate predicateWithFormat:@"uid contains[cd] %@", myID];
NSPredicate *p4 = [NSPredicate predicateWithFormat:@"targetuser contains[cd] %@", targetedUser];
NSCompoundPredicate *pIntermediary1 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p2,]];
NSCompoundPredicate * pIntermediary2 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p3, p4,]];
NSCompoundPredicate *pFinal = [NSCompoundPredicate orPredicateWithSubpredicates:@[pIntermediary1, pIntermediary2]];
NSArray *filtered = [self.messages filteredArrayUsingPredicate:pFinal];
NSLog(@"WAT IS HERE 2 %@", filtered);
NSString *userName = filtered[indexPath.row][@"first name"];
cell.sendingUser.text = userName;
NSString *messageBody = filtered[indexPath.row][@"body"];
cell.messageDisplayed.text = messageBody;
NSString *timeReceived = filtered[indexPath.row][@"published at"];
cell.timeStamp.text = timeReceived;
}
return cell;
}
答案 0 :(得分:1)
根据您的代码,消息是一个字典数组,而不是数组字典。你在我的词典"中谈到" First数组,但那是错的。你只有1个数组。
如果数据不是字典数组,那么您发布的代码会崩溃。
我建议您记录messages
结构,看看它真正包含的内容。
您在编辑中发布的代码很乱。你应该不构建谓词和排序内容以响应任何表视图数据源方法。您应该在调用表视图时构建数据模型,然后不管它。
如果您有一个节表视图,则应该返回1以响应numberOfSections调用,并且所有单元格的数据将来自单个数组。
您的numberOfRowsInSection调用应该只查找该部分的数组中的项目数。
cellForRowAtIndexPath方法应从该(一)部分中的该行的数组中获取数据项,并使用它来填充单元格。
答案 1 :(得分:1)
@Duncan关于一个数组是正确的。你有一个字典数组。
现在你还需要看看
func tableView(UITableView, numberOfRowsInSection: Int)
和
func numberOfSections(in: UITableView)
如果你的数组中有numberOfSections ==项目数,则indexpath.row将始终为0.我怀疑这可能是问题所在。