簡體   English   中英

將數據從表視圖單元格嵌入並傳輸到多個Uitextfields

[英]Embedding and transferring data from table view cell to multiple Uitextfields

我有一個與表視圖鏈接的搜索顯示控制器,因此當用戶搜索時,他們可以選擇所需的選項。 在他們選擇了想要的搜索詞之后,表格視圖單元將需要將數據解包到三個單獨的文本字段中,因此我將需要知道如何將其嵌入表格視圖單元中。

這是我的搜索顯示控制器代碼:

@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

“ [[Candy candyOfCategory:@“ Good” name:@“ 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];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM