繁体   English   中英

Obj-C-如果NSMutableArray多次包含同一字段,则从表视图中删除单元格

[英]Obj-C - Remove cell from tableview if NSMutableArray contains same field more than once

如果userName“ Brittany”在我的NSMutableArray中出现多次,我想从TableView中删除UITableViewCells。 例如,仅应显示第一个显示用户名“ Brittany”的单元格。 如果我的阵列self.messages中包含用户名“布列塔尼”不止一次,我只想显示第一个单元格。 我怎样才能做到这一点? 到目前为止,请参见下面的代码:

ViewController.m

- (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];


        NSMutableArray* NameDictionary=[[NSMutableArray alloc] init];

        NSString* PreviousName;

        int oneTime=0;

        for(int i=0;i<[self.messages count];i++){

            NSDictionary *fromUser = [self.messages objectAtIndex: i];

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

            if(oneTime==0)
            {
                NSLog(@"TEST!");
                PreviousName=userName;

                [NameDictionary addObject:[self.messages objectAtIndex: i]];

                oneTime=1;
            }

            if(oneTime!=0)
            {

                if([PreviousName isEqualToString: userName])
                {

                    NSLog(@"TEST 1!");
                }

                else{

                    [NameDictionary addObject:[self.messages objectAtIndex: i]];
                    NSLog(@"TEST 2!");
                }

                PreviousName=userName;
            }
        }


    }




    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"];

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

    [cell.receivedStamp setText:timeReceived];

    return cell;

}

您不应尝试将非重复项从阵列显示到表视图中。 那是灾难的秘诀。

而是创建一个removeDuplicates函数,该函数接受一个输入数组并返回一个新数组。

让函数创建一个空的可变集。 从头到尾循环遍历源数组。 对于源数组中的每个对象,如果它不在集合中,则将其附加到测试集和目标数组中。 如果它在测试集中,请跳过它。

请注意,只有在数组中的对象实现isEqual方法并返回有意义的值时,以上内容才有效。 默认情况下,如果您要比较完全相同的对象,则我相信通用NSObjects返回isEqual == true 其他类(如NSStringNSNumber实现了一个isEqual方法,该方法实际上比较该值。 如果使用字符串userName之类的对象比较复杂的对象,则可能需要创建具有isEqual的自定义实现的自定义对象类型(例如UserObject)。

那应该足以让您入门。 如果卡住了,请发布代码,以解释所遇到的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM