gpt4 book ai didi

xcode - iOS:表格 View 中的搜索栏

转载 作者:行者123 更新时间:2023-11-29 05:03:50 25 4
gpt4 key购买 nike

我正在 TableView 中创建一个搜索栏,但我对此方法委托(delegate)有问题,因为我用另一个数组内的数组填充 TableView ...我显示了我的代码:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
ProgramAppDelegate *appDelegate = (ProgramAppDelegate *)[[UIApplication sharedApplication] delegate];
[tableData removeAllObjects];// remove all data that belongs to previous search
if([searchText isEqualToString:@""] || searchText==nil){
[myTableView reloadData];
return;
}
NSInteger counter = 0;
for(NSString *name in appDelegate.globalArray )
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSRange r = [name rangeOfString:searchText];
if(r.location != NSNotFound)
{
if(r.location== 0)//that is we are checking only the start of the names.
{
[tableData addObject:name];
}
}
counter++;
[pool release];
}
[myTableView reloadData];
}

您可以看到代码“for(NSString *name in appDelegate.globalArray )”它不起作用,因为我用这个全局数组内的数组元素填充 TableView ,我做了一个例子

在我的表格 View 的一行中有一个uitableviewcell,里面有四个标签;我用这个 globalArray 的字符串编写这些标签,但以这种方式:

[cell.label1 setText:[[appDelegate.globalArray objectAtIndex:indexPath.row]objectAtIndex:1]];
[cell.label2 setText:[[appDelegate.globalArray objectAtIndex:indexPath.row]objectAtIndex:2]];
[cell.label3 setText:[[appDelegate.globalArray objectAtIndex:indexPath.row]objectAtIndex:3]];
[cell.label4 setText:[[appDelegate.globalArray objectAtIndex:indexPath.row]objectAtIndex:4]];

然后在搜索栏的委托(delegate)方法中,代码“for(NSString *name in appDelegate.globalArray )”不起作用,我如何更改我的代码?

** 我并不是说我只想检查 LABEL1 的搜索

最佳答案

这里的问题是 globalArray 是一个数组的数组。所以循环应该是这样的。

for(NSArray *rowArray in appDelegate.globalArray )
{
for ( NSString *name in rowArray ) {
// Do your processing here..
}
}

使用tableData

在您的tableView:cellForRowAtIndexPath:中,执行此操作

[cell.label1 setText:[[tableData objectAtIndex:indexPath.row]objectAtIndex:1]];
[cell.label2 setText:[[tableData objectAtIndex:indexPath.row]objectAtIndex:2]];
[cell.label3 setText:[[tableData objectAtIndex:indexPath.row]objectAtIndex:3]];
[cell.label4 setText:[[tableData objectAtIndex:indexPath.row]objectAtIndex:4]];

在您的numberOfSectionsInTableView:中,return 1;

在您的tableView:numberOfRowsInSection:中,return [tableData count];

您应该包含一个方法,当用户停止搜索时,该方法会将搜索数据重置为原始内容。

- (void)resetSearchData {
// Get appDelegate first.

self.tableData = [NSArray arrayWithArray:appDelegate.globalArray];
}

关于xcode - iOS:表格 View 中的搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6174073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com