gpt4 book ai didi

ios - Objective-C : Only return 1 cell (top cell) if cells proceeding contain the same UILabel values?

转载 作者:行者123 更新时间:2023-11-28 21:04:38 27 4
gpt4 key购买 nike

我的表格 View 中前 3 个单元格有一个标签,上面写着“松鼠”这个词。有没有办法做到这一点,如果 UILabel 在我的表格中的多个单元格中显示“Squirrels”,则只显示三个单元格中的第一个单元格?

E.g. if UILabel userName in tableviewCell is equal to @"Squirrels", only show one table view cell in the table that contains Squirrels in the UILabel

希望这是有道理的。提前致谢!


编辑: 我已成功检索到包含多个常见“名称”值的第一个数组(请参阅下面的代码编辑)。也就是说,当我尝试在我的 TableView 中显示这些值 (firstFoundObject) 时,出现以下崩溃错误:

-[__NSDictionaryI objectAtIndex:]: unrecognized selector sent to instance 0x1c01a5a20 2017-10-03 23:01:51.728128-0700 pawswap[623:85420] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI objectAtIndex:]: unrecognized selector sent to instance 0x1c01a5a20'

ViewController.m

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

NSString *nodeTitle = self.messages[0][@"name"];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", nodeTitle];
NSArray *filteredArray = [self.messages filteredArrayUsingPredicate:predicate];
id firstFoundObject = nil;
firstFoundObject = filteredArray.count > 0 ? filteredArray.firstObject : nil;

NSMutableArray *firstObjects = firstFoundObject;

return [firstObjects count];


}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

NSString *nodeTitle = self.messages[0][@"name"];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", nodeTitle];
NSArray *filteredArray = [self.messages filteredArrayUsingPredicate:predicate];
id firstFoundObject = nil;
firstFoundObject = filteredArray.count > 0 ? filteredArray.firstObject : nil;

NSMutableArray *firstObjects = firstFoundObject;

static NSString *PointsTableIdentifier = @"MyMessagesCell";

MyMessagesCell *cell = (MyMessagesCell *)[tableView dequeueReusableCellWithIdentifier:PointsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyMessagesCell" owner:self options:nil];
cell = [nib objectAtIndex:0];


}




NSDictionary *receivedSubjectLine = [firstObjects objectAtIndex:indexPath.row];

NSString *messageSubject = [receivedSubjectLine objectForKey:@"node_title"];


[cell.subjectLine setText:messageSubject];



NSDictionary *fromUser = [firstObjects objectAtIndex:indexPath.row];

NSString *userName = [fromUser objectForKey:@"name"];

[cell.senderName setText:userName];




NSDictionary *receivedBody = [firstObjects objectAtIndex:indexPath.row];

NSString *messageBody = [receivedBody objectForKey:@"body"];

[cell.fullMessage setText:messageBody];




NSDictionary *messageReceived = [firstObjects objectAtIndex:indexPath.row];

NSString *timeReceived = [messageReceived objectForKey:@"published at"];

NSLog(@"Message Received at %@", timeReceived);

[cell.receivedStamp setText:timeReceived];




return cell;

}

上一个

ViewController.m

#pragma mark - Table view data source

- (int)numberOfSectionsInTableView: (UITableView *)tableview
{
return 1;
}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.messages count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *PointsTableIdentifier = @"MyMessagesCell";

MyMessagesCell *cell = (MyMessagesCell *)[tableView dequeueReusableCellWithIdentifier:PointsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyMessagesCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

NSDictionary *receivedSubjectLine = [self.messages objectAtIndex:indexPath.row];
NSString *messageSubject = [receivedSubjectLine objectForKey:@"node_title"];
[cell.subjectLine setText:messageSubject];

NSDictionary *fromUser = [self.messages objectAtIndex:indexPath.row];
NSString *userName = [fromUser objectForKey:@"name"];
[cell.senderName setText:userName];

NSDictionary *receivedBody = [self.messages objectAtIndex:indexPath.row];
NSString *messageBody = [receivedBody objectForKey:@"body"];
[cell.fullMessage setText:messageBody];

NSDictionary *messageReceived = [self.messages objectAtIndex:indexPath.row];
NSString *timeReceived = [messageReceived objectForKey:@"published at"];
[cell.receivedStamp setText:timeReceived];

return cell;
}

最佳答案

基本上,您遇到的问题是由于 firstObject 是 Dictionary 类型,而您将其类型转换为 NSMutableArray。请检查以下行:

id firstFoundObject = nil; firstFoundObject = filteredArray.count > 0 ? filteredArray.firstObject : nil;

如果您看到您在应用程序中将 filteredArray.firstObject 作为 Dictionary,您在 firstFoundObject 中捕获了它,但后来您将其设为 NSMutableArray,请在此处键入:

NSMutableArray *firstObjects = firstFoundObject;

后来当你试图到达这里时,它崩溃了

NSDictionary *receivedSubjectLine = [firstObjects objectAtIndex:indexPath.row];

您的代码的正确 - 基本 - 版本应如下所示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *PointsTableIdentifier = @"MyMessagesCell";
MyMessagesCell *cell = (MyMessagesCell *)[tableView dequeueReusableCellWithIdentifier:PointsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyMessagesCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

[cell.subjectLine setText:[self.recvMessage objectForKey:@"node_title"]];
[cell.senderName setText:[self.recvMessage objectForKey:@"name"]];
[cell.fullMessage setText:[self.recvMessage objectForKey:@"body"]];
[cell.receivedStamp setText:[self.recvMessage objectForKey:@"published at"]];

return cell;
}

虽然它没有优化,但它仍然可以为你工作。

计数问题:

NSMutableDictionary *firstObjects = firstFoundObject;
return [firstObjects count];

在你上面的代码中你有 numberOfRowsInSection 因为你有 firstFoundObject 作为字典所以当你调用 [firstObjects count] 这是一个有效的调用并且它返回字典中的键数。

您已将其修改为:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{

NSInteger rowCount = filteredArray.count;
self.recvMessage = rowCount? filteredArray.firstObject: nil;
return rowCount? 1: 0;

}

并且您拥有实际存储过滤后对象的新数据。

@property (nonatomic) NSDictionary  *recvMessage;

希望这对您有所帮助。

关于ios - Objective-C : Only return 1 cell (top cell) if cells proceeding contain the same UILabel values?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46483246/

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