问题描述
我有一个 UITableViewController,它使用 NSMutableArray 作为它的数据源.该数组最初通过调用 Web 服务检索数据并填充 NSMutableArray 填充到 viewDidLoad 方法中.然后,用户可以选择输入搜索词(在 UISearchBar 中)并按 Search 重新运行 Web 服务并重新填充表格列表.此时,我需要清空数组并根据 Web 服务的结果重新填充表.我的问题是在不破坏表格视图的重新绘制的情况下,正确的方法是什么?我可以看到 2 个选项:
I have a UITableViewController which uses a NSMutableArray for it's datasource. The array is initially populated in the viewDidLoad method by calling a web service to retrieve the data and populate the NSMutableArray. The user then has the option to enter a search term (in a UISearchBar) and press Search to rerun the web service and repopulate the table list. At this point I need to empty the array and repopulate the table from the results of the web service. My question is what is the correct way to do this without upsetting the repainting of the table view? I can see 2 options :
调用[listArray removeAllObjects];然后 [[self tableView] reloadData];在重新填充数组之前.
Call [listArray removeAllObjects]; then [[self tableView] reloadData]; before repopulating the array.
当我将列表限制为最多 200 行时,用 200 个项目初始化数组,然后在运行搜索时使用 [listArray replaceObjectAtIndex: 替换每行,而不是删除和重新添加.这将需要一个 int 变量来保持返回的行数,并在 'tableView numberOfRowsInSection' 方法中使用它,以便 tableview 只显示返回的行数.
As I restrict the list to a maximum of 200 rows, initialise the array with 200 items and then instead of removing and re-adding when the search is run, use [listArray replaceObjectAtIndex: to replace each row. This would then require an int variable to keep the number of rows returned and use this in the 'tableView numberOfRowsInSection' method so the tableview only displays the number of rows returned.
希望这是有道理的!我问这个问题是因为我在重新加载列表时遇到了一些间歇性错误 (EXC_BAD_ACCESS),我确信这与我清空和重新加载列表的方式有关,因此可以使用一些关于最佳方法的建议.
Hope that makes sense! I'm asking the question because I've had some intermittent errors (EXC_BAD_ACCESS) when re-loading the list and I'm convinced it's to do with how I'm emptying and reloading the list so could use some advice on the best approach.
感谢任何帮助,
乔纳森
更新:
在 viewDidLoad 中初始化数组的代码:
Code to initialise array in viewDidLoad :
tableListDataArray = [[NSMutableArray alloc] init];
检索到数据后,将其添加到数组中,如下所示:
Once data is retrieved, it is added to the array as follows :
CustSuppListItem *custSuppItem = [[CustSuppListItem alloc] init];
[custSuppItem setAcCode:[jsonCustSuppRecord getStringForKey:@"acCode"]];
[custSuppItem setAcCompany:[jsonCustSuppRecord getStringForKey:@"acCompany"]];
[custSuppItem setAcContact:[jsonCustSuppRecord getStringForKey:@"acContact"]];
[custSuppItem setOsBalBase:[jsonCustSuppRecord getDoubleForKey:@"osBalBase"]];
[custSuppItem setAcAccStatus:[jsonCustSuppRecord getIntForKey:@"acAccStatus"]];
[tableListDataArray addObject:custSuppItem];
[custSuppItem release];
数组在dealloc方法中释放如下:
The array is released in the dealloc method as follows :
[tableListDataArray release];
推荐答案
我个人会选择选项 1.你应该能够使用
I personally would go with option 1. You should just be able to use
[tableView reloadData]
没有自我".我相信 reloadData 也会调用 cellForRowAtIndexPath 方法在使用之前不要忘记初始化你的 NSMutableArray.
without the "self". reloadData will also call the cellForRowAtIndexPath method I believeDon't forget to initialize your NSMutableArray before using it.
myMutableArray = [[NSMutableArray alloc] initWithCapacity:9001];
这篇关于目标 C - 使用 NSMutableArray 清空和重新加载 UITableViewController 的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!