我有一个数据列表,我从中搜索一些项目然后选择其中一个扩展所选单元格的数据。然后,当我清除搜索栏中的搜索文本时,我希望显示原始列表。我能够显示原始列表中的所有项目,但在搜索期间选择的单元格不会更新。附加快照和代码以便更好地理解:
请注意,第一张图片中的第3行有文字" A.B.道"而第三张图像中的同一行使用与第二张图像中相同的单元格(它应该更新为" AB Road")我正在使用自定义单元格并尝试每次创建一个新单元格而不是重用现有单元格。这没有用。
代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = [self.branchList objectAtIndex:[indexPath row]];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CGSize labelSize = [cellText boundingRectWithSize:CGSizeMake(tableView.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:cellFont} context:nil].size;
CGFloat cellHeight = labelSize.height + 20;
return [self.expandedCells containsObject:indexPath] ? cellHeight * 5 : cellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *stateListCellId = @"stateList";
BranchDetailsTableViewCell *stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];
if (!stateListCell) {
[tableView registerNib:[UINib nibWithNibName:@"BranchDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:stateListCellId];
stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];
}
return stateListCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.expandedCells containsObject:indexPath]) {
[self.expandedCells removeObject:indexPath];
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
else {
[self.activityIndicator showActivityIndicatorForView:self.navigationController.view];
self.selectedIndexPath = indexPath;
[self.expandedCells addObject:indexPath];
[self getDataFromService];
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(BranchDetailsTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[self displayDataOnTheCell];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
if ([self.tableView numberOfRowsInSection:0] != [self.branchListCopy count]) {
[self.expandedCells removeAllObjects];
// Copy the original list to display.
self.branchList = self.branchListCopy;
[self.tableView reloadData];
}
}
看起来它只是没有再次渲染的单元格,因为当我调试时,我确实看到" A.B。道"在数组中,它是没有显示它的单元格。我试着打电话给#34; [cell setNeedsDisplay]"并且还创建新单元格而不是重复使用,但没有任何帮助。还有什么可以帮助?
谢谢!
答案 0 :(得分:1)
我能够通过在didSelectRowAtIndexPath方法中用[tableview reloadData]
替换以下代码来保持其工作原理。
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
最终工作解决方案:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = [self.branchList objectAtIndex:[indexPath row]];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CGSize labelSize = [cellText boundingRectWithSize:CGSizeMake(tableView.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:cellFont} context:nil].size;
CGFloat cellHeight = labelSize.height + 20;
return [self.expandedCells containsObject:indexPath] ? cellHeight * 5 : cellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *stateListCellId = @"stateList";
BranchDetailsTableViewCell *stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];
if (!stateListCell) {
[tableView registerNib:[UINib nibWithNibName:@"BranchDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:stateListCellId];
stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];
}
return stateListCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.expandedCells containsObject:indexPath]) {
[self.expandedCells removeObject:indexPath];
[self.tableView reloadData];
}
else {
[self.activityIndicator showActivityIndicatorForView:self.navigationController.view];
self.selectedIndexPath = indexPath;
[self.expandedCells addObject:indexPath];
[self getDataFromService];
}
}
答案 1 :(得分:0)
我认为问题在于您为每个单元使用相同的单元标识符。尝试使用不同的单元格标识符:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//NSString *stateListCellId = @"stateList";
NSString *cellIdentifier = [NSString stringWithFormat:@"cell%ld",(long)indexPath.row];
BranchDetailsTableViewCell *stateListCell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
if (!stateListCell) {
[tableView registerNib:[UINib nibWithNibName:@"BranchDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:stateListCellId];
stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];
}
return stateListCell;
}