【发布时间】:2011-09-04 16:03:00
【问题描述】:
我在 tableview 中创建了一个搜索栏,但是我在使用这个方法委托时遇到了问题,因为我用另一个数组中的数组填充了 table view...我显示了我的代码:
- (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)”它不起作用,因为我用这个全局数组中的数组元素填充表格视图,我举个例子
在我的表格视图的一行中有一个 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]];
然后在搜索栏的委托方法中,代码“for(NSString *name in appDelegate.globalArray)”不起作用,我该如何更改我的代码?
** 我并不是说我只想检查 LABEL1 以进行搜索
【问题讨论】:
标签: xcode ios uitableview uisearchbar