如何为 UITableview 中的每个部分制作不同的搜索字段?
How to make different search field for every sections in UITableview?
假设我想为每个部分实现带有搜索字段的 UITableview。如果我在第一部分中搜索结果行,则应仅在第一部分中更新。同样,如果我在第二部分中搜索,第二部分结果行应该更新,其余部分依此类推。
我已经在每个部分搜索字段中实现了,但是当我重新加载部分或为部分重新加载行时它会重新加载部分。
我得到了解决方案。请找到下面的代码。因为我正在为我的视图控制器使用自动布局和情节提要,所以我必须将它作为 iboutlets,截至目前我只有 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;
}
}
假设我想为每个部分实现带有搜索字段的 UITableview。如果我在第一部分中搜索结果行,则应仅在第一部分中更新。同样,如果我在第二部分中搜索,第二部分结果行应该更新,其余部分依此类推。
我已经在每个部分搜索字段中实现了,但是当我重新加载部分或为部分重新加载行时它会重新加载部分。
我得到了解决方案。请找到下面的代码。因为我正在为我的视图控制器使用自动布局和情节提要,所以我必须将它作为 iboutlets,截至目前我只有 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;
}
}