我有一个数据列表,我从中搜索一些项目,然后选择其中一个来展开所选单元格。然后,当我清除搜索栏中的搜索文本时,我希望显示原始列表。我能够显示原始列表中的所有项目,但搜索过程中选择的单元格未更新。附上快照和代码以便更好地理解:
请注意,第一张图片中的第 3 行有文本“A.B. Road”,而第三张图片中的同一行使用与第二张图片中相同的单元格(它应该更新为“A.B. 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. Road”,它是没有显示它的单元格。我尝试调用“[cell setNeedsDisplay]”并始终创建新单元而不是重用,但没有任何帮助。还有什么可以帮助?
谢谢!
最佳答案
我可以通过在 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];
}
}
关于ios - UITableViewCell 无法正确显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40570740/