失去我的对象

时间:2014-11-12 11:33:29

标签: ios objective-c uitableview

UITableView UISearchController填充了我的自定义对象。当我找到一些对象时,我称之为reloadData方法。但是我看不到任何物体,因为它们总是丢失。我有一些组,它们转换成各个部分,每个组都包含对象,这些对象可以转换为部分行。

有一些代码和日志:

UISearchControllerDelegate方法实现:

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    // Searching

    // Put all found objects into findedObjects
    // Sort findedObjects to groups and put them into findedGroups

    findedObjects = searchResults;

    [findedGroups removeAllObjects];

    for (InfoGroup *buffGroup in groups)
    {
        InfoGroup *newGroup = [InfoGroup groupWithName:buffGroup.groupName andObjects:nil isMainGroup:buffGroup.mainGroup];
        newGroup.groupImageName = buffGroup.groupImageName;
        newGroup.groupPeoples = buffGroup.groupPeoples;

        BOOL flag = NO;

        for (InfoObject *obj in findedObjects) //sort objects
        {
            if ([obj.objectGroup.groupName isEqualToString:newGroup.groupName])
            {
                [newGroup.groupObjects addObject:obj];
//                obj.objectGroup = newGroup;
                NSLog(@"count of groupObjects: %li",[newGroup.groupObjects count]);
                flag = YES;
            }
        }

        if (flag) // if new group have more than 0 objects, put her in array
        {
            [findedGroups addObject:newGroup];
        }
    }

    NSLog(@"found %li groups and summary %li objects", [findedGroups count],[findedObjects count]);

    [self.tableView reloadData];
}

UITableView数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger count = ([findedGroups count] > 0) ? [findedGroups count] : [groups count];

    NSLog(@"number of sections: %li", count);

    return count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = ([findedGroups count] > 0) ? [[findedGroups[section] groupObjects] count] : [[groups[section] groupObjects] count];

    NSLog(@"number of rows: %li in section: %li", count, section);

    return count;
}

有非常有趣的日志(在搜索字段中输入内容时,已清除):

found 2 groups and summary 5 objects <----- 5 objects
number of sections: 2 <----- HERE TWO
number of rows: 0 in section: 1 <----- here 0 objects
number of rows: 0 in section: 0 <----- here 0 too

found 1 groups and summary 1 objects
number of sections: 1
number of rows: 0 in section: 0

found 1 groups and summary 1 objects
number of sections: 1
number of rows: 0 in section: 0

所以,我在numberOfSectionsInTableViewnumberOfRowsInSection之间丢失了我的对象。

有什么建议吗?可能是我错过了什么,但我无法找到。

抱歉焦虑。真是愚蠢的错误浪费了几个小时。

1 个答案:

答案 0 :(得分:0)

仅用于标记已解决

我解决了这个问题。因为我把nil放在这里

InfoGroup *newGroup = [InfoGroup groupWithName:buffGroup.groupName andObjects:nil isMainGroup:buffGroup.mainGroup];
groupObjects array was not initialized.

所以我只是针对那种情况的更新方法:

+(instancetype)groupWithName:(NSString*)initialName andObjects:(NSMutableArray*)initialObjects isMainGroup:(BOOL)initialMain
{
    if (initialObjects)
    {
        [ig setGroupObjects:initialObjects];
    } else
    {
        ig.groupObjects = [NSMutableArray array];
    }
}