Ios 表视图的搜索栏
我希望在一个视图控制器中有两个表,并且只在其中一个中进行搜索。我该怎么办 我知道如何实现搜索栏,但我不知道如何在表之间进行区分。我试着用标签来做这件事,但不起作用 任何建议都会有帮助Ios 表视图的搜索栏,ios,objective-c,uitableview,uisearchbar,Ios,Objective C,Uitableview,Uisearchbar,我希望在一个视图控制器中有两个表,并且只在其中一个中进行搜索。我该怎么办 我知道如何实现搜索栏,但我不知道如何在表之间进行区分。我试着用标签来做这件事,但不起作用 任何建议都会有帮助 UITableViewCell *cell ; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIde
UITableViewCell *cell ;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
if (tableView.tag==1) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = self.tableData[indexPath.row];
}}
else if (tableView.tag==0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [historique objectAtIndex:indexPath.row];
}
return cell;
}
我也试着这样做:
即使我像这样
self.tableSource= self.searchDisplayController.searchResultsTableView; the result is the same
使用变量来处理这两个表 然后,您只能对所需的操作执行操作 例如:
@interface MyClass
@property (nonatomic, strong) UITableView *searchableTableView;
@property (nonatomic, strong) UITableView *nonSearchableTableView;
@end
您需要为两个表视图创建两个属性
@property (strong, nonatomic) IBOutlet UITableView *table1;
@property (strong, nonatomic) IBOutlet UITableView *table2;
然后实现表视图的委托方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if([tableView isEqual:table1])
{
return YOUR_VALUE;
}
else
{
return YOUR_VALUE;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([tableView isEqual:table1])
{
return [array1 count]
}
else
{
return [array2 count];
}
用同样的方法实现委托的CellForRowatineXpath方法。我这样做了,但是当我要搜索某个对象时,会调用CellForRowatineXpath,并且将执行搜索的表是不可搜索的。请查看您如何进行搜索。你自己实现了吗?因为我试过了,没有任何问题。我通过代码调用_searchableTableTableView上的cellForRowAtIndexPath。为两个表视图创建属性,并使用属性名称区分表。就像上面的代码一样——如果([tableView isEqual:table1])尝试使用断点进行调试,我认为您缺少了一些东西