gpt4 book ai didi

ios - Obj-C-显示对应数组中每个单元格的正确数据

转载 作者:行者123 更新时间:2023-12-01 19:45:35 26 4
gpt4 key购买 nike

在我的tableView中,我试图为其中swapvalue等于0的每个字典显示某种类型的单元格。也就是说,出于某种原因,下面的代码仅获取数组中第一个字典的值(又名:,第一个字典中的swapvalue的值为0,因此,我的表显示所有单元格,就好像后面的数组也具有swapvalue一样-即使它们没有)。如何更改此设置,以便每个单元格都从其对应的字典中获取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;

}

最佳答案

@Duncan对一个数组是正确的。您有一组字典。
现在您还需要查看func tableView(UITableView, numberOfRowsInSection: Int)func numberOfSections(in: UITableView)如果您的数组中的项目数为numberOfSections ==,则indexpath.row将始终为0。我怀疑这可能是问题所在。

关于ios - Obj-C-显示对应数组中每个单元格的正确数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48728222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com