将表格视图单元格中的数据嵌入并传输到多个Uitextfields

时间:2014-11-23 05:33:48

标签: ios objective-c xcode search

我有一个与表格视图相关联的搜索显示控制器,因此当用户搜索时,他们可以选择他们想要的选项。在他们选择了他们想要的搜索词之后,表格视图单元格需要将数据解压缩到三个单独的文本字段中,因此我需要知道如何将其嵌入到表格视图单元格中。

以下是搜索显示控制器的代码:

@synthesize candyArray;
@synthesize filteredCandyArray;
@synthesize candySearchBar;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Don't show the scope bar or cancel button until editing begins
    [candySearchBar setShowsScopeBar:NO];
    [candySearchBar sizeToFit];

    // Hide the search bar until user scrolls up
    CGRect newBounds = [[self tableView] bounds];
    newBounds.origin.y = newBounds.origin.y + candySearchBar.bounds.size.height;
    [[self tableView] setBounds:newBounds];

    /*** Sample Data for candyArray ***/

    candyArray = [NSArray arrayWithObjects:
                  [Candy candyOfCategory:@"Good" name:@"Bath Towel - Kmart"],
                  [Candy candyOfCategory:@"Good" name:@"Bath Towel - Wallmart"],
                  [Candy candyOfCategory:@"Good" name:@"Bath Towel - Target"],
                  [Candy candyOfCategory:@"Better" name:@"Bath Towel - Khols"],
                  [Candy candyOfCategory:@"Better" name:@"Bath Towel - JCpenny"],
                  [Candy candyOfCategory:@"Best" name:@"Bath Towel - Dillards"],
                  [Candy candyOfCategory:@"Best" name:@"Bath Towel - Macy's"],
                  [Candy candyOfCategory:@"Good" name:@"Hand Towel - Kmart"],
                  [Candy candyOfCategory:@"Good" name:@"Hand Towel - Wallmart"],
                  [Candy candyOfCategory:@"Good" name:@"Hand Towel - Target"][Candy candyOfCategory:@"Best" name:@"Bath Canister - Macy's"], nil];

    // Initialize the filteredCandyArray with a capacity equal to the candyArray's capacity
    filteredCandyArray = [NSMutableArray arrayWithCapacity:[candyArray count]];

    // Reload the table
    [[self tableView] reloadData];


}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Check to see whether the normal table or search results table is being displayed and return the count from the appropriate array
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [filteredCandyArray count];
    }
    else
    {
        return [candyArray count];
    }
}

- (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];
    }

    // Create a new Candy Object
    Candy *candy = nil;

    // Check to see whether the normal table or search results table is being displayed and set the Candy object from the appropriate array
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        candy = [filteredCandyArray objectAtIndex:[indexPath row]];
    }
    else
    {
        candy = [candyArray objectAtIndex:[indexPath row]];
    }

    // Configure the cell
    [[cell textLabel] setText:[candy name]];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    return cell;
}

#pragma mark - TableView Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Perform segue to candy detail
    [self performSegueWithIdentifier:@"candyDetail" sender:tableView];
}

#pragma mark - Segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ( [[segue identifier] isEqualToString:@"candyDetail"] ) {
        UIViewController *candyDetailViewController = [segue destinationViewController];

        // In order to manipulate the destination view controller, another check on which table (search or normal) is displayed is needed
        if(sender == self.searchDisplayController.searchResultsTableView) {
            NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            NSString *destinationTitle = [[filteredCandyArray objectAtIndex:[indexPath row]] name];
            [candyDetailViewController setTitle:destinationTitle];
        }
        else {
            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            NSString *destinationTitle = [[candyArray objectAtIndex:[indexPath row]] name];
            [candyDetailViewController setTitle:destinationTitle];
        }

    }
}

- (IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark Content Filtering

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    // Update the filtered array based on the search text and scope.

    // Remove all objects from the filtered search array
    [self.filteredCandyArray removeAllObjects];

    // Filter the array using NSPredicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
    NSArray *tempArray = [candyArray filteredArrayUsingPredicate:predicate];

    if(![scope isEqualToString:@"All"]) {
        // Further filter the array with the scope
        NSPredicate *scopePredicate = [NSPredicate predicateWithFormat:@"SELF.category contains[c] %@",scope];
        tempArray = [tempArray filteredArrayUsingPredicate:scopePredicate];
    }

    filteredCandyArray = [NSMutableArray arrayWithArray:tempArray];
}


#pragma mark - UISearchDisplayController Delegate Methods

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    // Tells the table data source to reload when text changes
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    // Tells the table data source to reload when scope bar selection changes
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

#pragma mark - Search Button



@end

1 个答案:

答案 0 :(得分:0)

  

“[Candy candyOfCategory:@”Good“名称:@”Bath Towel - Kmart“],”将作为数据,我需要一个nsstring链接到文本字段。

我将假设您在目的地(明细)视图控制器中需要这个?

步骤1:创建一个可用作自定义视图控制器的UIViewController子类

第2步:在自定义VC标头中声明@property个对象,例如:

@property (strong, nonatomic) NSString* category;
@property (strong, nonatomic) NSString* name;

(或者您甚至可以使用自定义的Candy类!)

第3步:而不是将新目标视图控制器声明为UIViewController,而不是将其声明为CandyDetailViewController(或您喜欢的任何名称):

CandyDetailViewController *candyDetailViewController = [segue destinationViewController];

第4步:在您设置目标标题的位置下方,您现在可以设置属性。

您的prepareForSegue方法现在应该类似于:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ( [[segue identifier] isEqualToString:@"candyDetail"] ) {
        CandyDetailViewController *candyDetailViewController = [segue destinationViewController];

        Candy *candy;

        if(sender == self.searchDisplayController.searchResultsTableView) {
            NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            candy = [filteredCandyArray objectAtIndex:[indexPath row]];
            NSString *destinationTitle = [[filteredCandyArray objectAtIndex:[indexPath row]] name];
        }
        else {
            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            candy = [filteredCandyArray objectAtIndex:[indexPath row]];
            NSString *destinationTitle = [[candyArray objectAtIndex:[indexPath row]] name];
        }
        candyDetailViewController.name = candy.name;
        candyDetailViewController.category = candy.category;
        [candyDetailViewController setTitle:destinationTitle];
    }
}