[ios]嵌入和将数据从表视图单元格传送到多个 Uitextfields

发布时间: 2017/3/28 23:14:34
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

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

这里是我的搜索显示控制器的代码︰

@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:

"[糖果 candyOfCategory:@"Good"name:@"Bath 毛巾 — — 凯马特"],"将作为数据,我需要 nsstring 链接到文本字段。

我要去假设需要在您的目的地 (详细信息) 视图控制器?

第 1 步︰创建一个子类可以使用作为您的自定义视图控制器的 UIViewController

第 2 步︰声明 @property 对象的自定义 VC 标题如︰

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

(或您甚至可以使用您自定义的糖果类太 !)

第 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];
    }
}
赞助商