我有ipad,它有tableView显示内容我希望当用户在搜索文本字段中输入任何数据时,匹配的记录应该在tableView中显示。
- (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
中答案 0 :(得分:0)
将此代码写入onSearchButtonClick方法
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;
}
}
答案 1 :(得分:0)
首先使用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];