[英]Lose my objects
I have UITableView
with UISearchController
filled with my custom objects. 我有UITableView
和UISearchController
充满我的自定义对象。 When i find some objects i call reloadData
method. 当我找到一些对象时,我调用reloadData
方法。 But i can't see any objects, because they are lost all the time. 但是我看不到任何物体,因为它们一直都在丢失。 I have groups, which translate into sections, each group contain objects, which translate into rows of section. 我有一些组,这些组可以转换成多个部分,每个组都包含一些对象,这些对象可以转换成多个部分的行。
There are some code and logs: 有一些代码和日志:
UISearchControllerDelegate
method implementation: 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
data source methods 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;
}
There are very interesting logs (while typing something in search field, cleaned): 有非常有趣的日志(在搜索字段中键入内容时,已清除):
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
So, i lose my objects somewhere between numberOfSectionsInTableView
and numberOfRowsInSection
. 因此,我在numberOfSectionsInTableView
和numberOfRowsInSection
之间的某个位置丢失了对象。
Any suggestions? 有什么建议么? May be i missing something, but i can't find what. 可能是我缺少了一些东西,但是我找不到。
Sorry for anxiety. 对不起,焦虑。 Really stupid mistake waste hours of time. 真正愚蠢的错误浪费时间。
Just for mark post as solved 仅用于标记发布已解决
I solve the problem. 我解决了问题。 Because i put nil here 因为我在这里放零
InfoGroup *newGroup = [InfoGroup groupWithName:buffGroup.groupName andObjects:nil isMainGroup:buffGroup.mainGroup];
groupObjects array was not initialized.
So i'm just update method for that case: 所以我只是针对这种情况的更新方法:
+(instancetype)groupWithName:(NSString*)initialName andObjects:(NSMutableArray*)initialObjects isMainGroup:(BOOL)initialMain
{
if (initialObjects)
{
[ig setGroupObjects:initialObjects];
} else
{
ig.groupObjects = [NSMutableArray array];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.