Ios 如何使用textfield从ipad的tableView中搜索数据
我有ipad,它有tableView显示内容,我希望当用户在搜索文本字段中输入任何数据时,匹配的记录应该显示在tableView中Ios 如何使用textfield从ipad的tableView中搜索数据,ios,search,tableview,Ios,Search,Tableview,我有ipad,它有tableView显示内容,我希望当用户在搜索文本字段中输入任何数据时,匹配的记录应该显示在tableView中 - (void)onSearchButtonClick { if (searchTextField.text == nil || searchTextField.text.length == 0) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message
- (void)onSearchButtonClick
{
if (searchTextField.text == nil || searchTextField.text.length == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You must fill the text to search first!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
[searchTextField resignFirstResponder];
}
以下是tableView中通常显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
CustomCell *cell = (CustomCell *)[tableView
d equeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell"
owner:self options:nil];
for(id oneObject in nib)
if ([oneObject isKindOfClass:[CustomCell class]])
cell = (CustomCell *)oneObject;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
ObjectData*obj;
obj=[appDelegate.detailArray objectAtIndex:indexPath.row];
type=obj.contentType;
content_Type=obj.contentType;
content_Source=obj.contentSource;
cell.text=Content_Source;
return cell;
}
我希望在单击搜索时,它应该匹配记录并显示在tableView中在onSearchButtonClick方法中编写此代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
CustomCell *cell = (CustomCell *)[tableView
d equeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell"
owner:self options:nil];
for(id oneObject in nib)
if ([oneObject isKindOfClass:[CustomCell class]])
cell = (CustomCell *)oneObject;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
ObjectData*obj;
obj=[appDelegate.detailArray objectAtIndex:indexPath.row];
type=obj.contentType;
content_Type=obj.contentType;
content_Source=obj.contentSource;
cell.text=Content_Source;
return cell;
}
NSString *firstLetter = @"";
NSInteger strlen=[textfield.text length];
for (NSString *ABCValue in ABCArray)
{
if(ABCValue.length >= stringlen)
firstLetter = [ABCValue substringToIndex:stringlen];
if([textfield.text.uppercaseString isEqualToString:firstLetter.uppercaseString])
{
[tableArray addObject:ABCValue]; //Store it in NSMutableArray
break;
}
}
首先,应该使用另一个可变数组来代替使用appdelegate数组填充表数据,该数组的起始位置将包含appdelegate数组的副本。 然后,在单击搜索按钮的方法中,您必须编写以下代码以在表中显示结果
//Allocate a new array....
NSMutableArray *newArray = [[[NSMutableArray alloc] init] autorelease];
//The search query....
NSString strQuery=[textfield.text length];
//Go through all the array for populating the table data.....
for (ObjectData *obj in appDelegate.detailArray)
{
//Check for the tye of the object.....
if ([obj isKindOfClass:[ObjectData class]])
{
//Now the contentSource of the object contains the search query then add the object to new array....
if([obj.contentSource rangeOfString:strQuery options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[newArray addObject:obj];
}
}
}
//Dont forget to populate the table with new array.....
yourArrayForpopulatingData = [newArray mutableCopy];
//after completion reload the tableview....
[aTableView reloadData];
ABCArray是一个数组,它将在其中搜索显示表数据的平均数组是的,ABCArray是一个在numberofRowsInTableView方法中使用的数组。为什么使用第一个字母为空在进入循环之前,我们将清除以前存储的文本。在循环中,它充满了一些单词。没有必要专注于这一点。只需检查您是否正在获取数据