假设我想实现 UITableview,每个部分都有搜索字段。如果我在第一部分中搜索,结果行应仅在第一部分中更新。同样,如果我在第二部分搜索,第二部分结果行应该更新,依此类推其余部分。
我已经实现了每个部分搜索字段,但当我重新加载部分或重新加载部分的行时,它会重新加载部分。
我找到了解决方案。请找到下面的代码。由于我在视图控制器中使用 AutoLayout 和 Storyboard,因此我采用了 IBOutlet,到目前为止我只有 2 个部分,因此我将其声明为静态,但如果您有更多部分,您可以通过创建视图来使其动态化。
// define in your .h file and take layout for the views and make it saperate from your main view container (outside of main view controller).
@property(strong,nonatomic)IBOutlet UITextField * txtSearchSection1;
@property(strong,nonatomic)IBOutlet UITextField * txtSearchSection2;
@property (strong, nonatomic) IBOutlet UIView *viewSection1;
@property (strong, nonatomic) IBOutlet UIView *viewSection2;
// Implement in your .m file
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40.0f;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section==1) {
return _viewSection1;
}else{
return _viewSection2;
}
}