2011-05-10 128 views
2

我正在创建一个应用程序,我想在其中显示一个文本框,当我在其中输入一个单词时,它将显示与该单词匹配的字典中的内容。现在,我想要显示从我的字典根据文本框中输入的词在表视图中生成的列表,并且该表视图应该位于具有文本框的相同视图控制器上。可以这样做。我的意思是可以通过滚动选项创建表格视图,以便用户可以滚动列表,然后选择他想要的单词。视图控制器上的表视图控制器

回答

4

是的,这是可能的。以IBOutlet为UITableView并将其连接起来。定义其数据源并将其委托给您的控制器。实现UITableViewDelegate到你的控制器并覆盖cellForRowAtIndex等所有方法。

//FilterDataViewController.h 
#import <UIKit/UIKit.h> 
@interface FilterDataViewController : UIViewController <UITableViewDelegate> 
{ 
    IBOutlet UITableView *tblView; 
    IBOutlet UITextField *txtFld; 

    NSMutableArray *arrSrch; 
    NSMutableArray *srchedData; 
} 
-(IBAction)srchBtnTapped:(id)sender; 
@end 

//FilterDataViewController.m 
#import "FilterDataViewController.h" 
@implementation FilterDataViewController 

-(IBAction)srchBtnTapped:(id)sender 
{ 
    if(![txtFld.text isEqualToString:@""]) 
    { 
     [srchedData removeAllObjects]; 
     for (NSString *allStrings in arrSrch) 
     { 
      NSComparisonResult result = [allStrings compare:txtFld.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [txtFld.text length])]; 
      if (result == NSOrderedSame) 
      { 
       [srchedData addObject:allStrings]; 
      } 
     } 
     [tblView reloadData]; 
    } 
} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    arrSrch = [[NSMutableArray alloc] initWithObjects:@"One",@"One Two",@"Two",@"Three",@"Four",@"One Five",@"Six",nil]; 
    srchedData = [[NSMutableArray alloc] init]; 
} 

#pragma mark - 
#pragma mark Table view data source 

// Customize the number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [srchedData count]; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [srchedData objectAtIndex:indexPath.row]; 
    // Configure the cell. 

    return cell; 
} 

@end 
+0

你可以给我一个示例代码或教程,请 – Christina 2011-05-10 05:28:34

+0

@Christina你可以按照http://chris-software.com/index.php/tag/uitableview/。如果您需要更多帮助,请留下评论我将为您准备演示并发布。 – 2011-05-10 05:35:04

+0

真是太好了,我真的需要帮助,请给我演示一下有一个表视图,然后在它下面的文本框,然后硬编码一些值,并显示在桌子上,这个应用程序是为iPAD.please如果你有时间可以请你给我这个....? – Christina 2011-05-10 05:41:54

1

这当然是可能的。创建一个基于视图的应用程序,并将你的表格视图和文本字段放在同一个视图中,你可以做任何你打算做的事情。

+0

我会自动获得滚动选项,或者我必须做任何特别的... – Christina 2011-05-10 05:30:20

+0

默认情况下,您将获得滚动。 – Krishnan 2011-05-10 05:30:49

+0

你可以给我任何示例代码,如果你有事端相关或任何链接,如果你知道... – Christina 2011-05-10 05:32:41

相关问题