【问题标题】:Tableview won't reload data after selecting row选择行后Tableview不会重新加载数据
【发布时间】:2018-03-16 07:04:16
【问题描述】:
当我选择 tableView 中的任何行时,数据应该被删除。我已经在 textField 中设置了选定的文本,它正在工作,但是 tableView 数据在选择后没有被清除,代码如下。
请帮帮我。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tblViewSearch deselectRowAtIndexPath:indexPath animated:YES];
TimeZoneCity *objCity = (TimeZoneCity *)[searchResult objectAtIndex:indexPath.row];
txtfieldTimeZone.text=objCity.timezone;
[searchResult removeAllObjects];
[self.tblViewSearch reloadData];
}
【问题讨论】:
标签:
objective-c
uitableview
uisearchcontroller
【解决方案1】:
如果你真的想删除表格视图,你可以试试这个。如果您使用界面构建器设置了表格视图,这是一个坏主意,因为在这种情况下它会为您创建。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tblViewSearch deselectRowAtIndexPath:indexPath animated:YES];
TimeZoneCity *objCity = (TimeZoneCity *)[searchResult objectAtIndex:indexPath.row];
txtfieldTimeZone.text=objCity.timezone;
[self.tblViewSearch removeFromSuperview];
}
更好的策略可能是隐藏它,然后在需要时再次显示。为了让它看起来更好,你也可以添加一个动画。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tblViewSearch deselectRowAtIndexPath:indexPath animated:YES];
TimeZoneCity *objCity = (TimeZoneCity *)[searchResult objectAtIndex:indexPath.row];
txtfieldTimeZone.text=objCity.timezone;
[UIView animateWithDuration:0.3 animations:^{
self.tblViewSearch.alpha = 0.0;
}];
}