Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从表视图中的单元格中检索信息?_Ios_Uitableview - Fatal编程技术网

Ios 如何从表视图中的单元格中检索信息?

Ios 如何从表视图中的单元格中检索信息?,ios,uitableview,Ios,Uitableview,我希望能够将标签的文本设置为从表视图中选择的文本 @interface STAdvancedBACViewController () @property (nonatomic, copy) NSDictionary *brand; @property (nonatomic, copy) NSArray *keys; @property (nonatomic, copy) NSMutableArray *filteredNames; @property (nonatomic, strong) U

我希望能够将标签的文本设置为从表视图中选择的文本

@interface STAdvancedBACViewController ()

@property (nonatomic, copy) NSDictionary *brand;
@property (nonatomic, copy) NSArray *keys;
@property (nonatomic, copy) NSMutableArray *filteredNames;
@property (nonatomic, strong) UISearchDisplayController *searchController;
@property (nonatomic, copy) NSDictionary *beerValues;
@property (nonatomic, copy) NSArray *beerKeys;

@end

@implementation STAdvancedBACViewController {

}


@synthesize brand, keys, filteredNames, searchController, beerValues, beerKeys;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UITableView *tableView = (id)[self.view viewWithTag:1];

    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"plist"];

    brand = [NSDictionary dictionaryWithContentsOfFile:path];

    keys = [[brand allKeys]sortedArrayUsingSelector:@selector(compare:)];


    filteredNames = [[NSMutableArray alloc]init];

    searchController = [[UISearchDisplayController alloc]init];

    searchController.searchResultsDataSource = self;

    NSString *path2 = [[NSBundle mainBundle] pathForResource:@"BEER2" ofType:@"plist"];

    beerValues = [NSDictionary dictionaryWithContentsOfFile:path2];

    beerKeys = [[beerValues allKeys] sortedArrayUsingSelector:@selector(compare:)];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView.tag == 1) {
        return [keys count];
    }
    else{
        return 1;
    }

}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (tableView.tag == 1) {
        NSString *key = keys[indexPath.section];
        NSArray *keyValues = brand[key];

        cell.textLabel.text = keyValues[indexPath.row];
    }
    else{
        cell.textLabel.text = filteredNames[indexPath.row];
    }
    return cell;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView.tag == 1) {
        NSString *key = keys[section];

        NSArray *keyValues = brand[key];

        return [keyValues count];
    }
    else {
        return [filteredNames count];
    }
}


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    if (tableView.tag ==1) {
        return keys;
    }
    else {
        return nil;
    }
}

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (tableView.tag ==1) {
        return keys[section];
    }
    else {
        return nil;
    }
}

#pragma mark

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

    [filteredNames removeAllObjects];

    if (searchString.length > 0) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search]   %@", self.searchBar.text];
        for (NSString *key in keys)  {
            NSArray *matches = [brand[key]filteredArrayUsingPredicate:predicate];
            [filteredNames addObjectsFromArray:matches];
        }
    }
    return YES;
}


- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

    _testLabel.text = keys[indexPath.row];
}

@end

出于某种原因,这不会返回单元格中的值,而是返回
Plist
文件中的一个字母。plist文件的组织方式是,有26个对象是字母表中每个字母的数组,然后是包含啤酒名称的数组。基本上,我想检索用户选择的啤酒名称。

虽然您的问题有点不清楚,但我猜您希望标签是用户选择的品牌名称。在
单元格中,您正在执行以下操作:

if (tableView.tag == 1) {
    NSString *key = keys[indexPath.section];
    NSArray *keyValues = brand[key];
    cell.textLabel.text = keyValues[indexPath.row];
}
else{
    cell.textLabel.text = filteredNames[indexPath.row];
}
_testLabel.text = keys[indexPath.row];
但在您的
中,您只是在执行以下操作:

if (tableView.tag == 1) {
    NSString *key = keys[indexPath.section];
    NSArray *keyValues = brand[key];
    cell.textLabel.text = keyValues[indexPath.row];
}
else{
    cell.textLabel.text = filteredNames[indexPath.row];
}
_testLabel.text = keys[indexPath.row];
您不应该在
中执行此操作吗

//Plan your logic accordingly. Its just a rough
if (tableView.tag == 1) {
    NSString *key = keys[indexPath.section];
    NSArray *keyValues = brand[key];
    _testLabel.text = keyValues[indexPath.row];
}
else{
    _testLabel.text = filteredNames[indexPath.row];
}

希望这有帮助:)

数组中存储了什么?每个元素都是字典,对吗?您试图访问它,因为它是单个字符串。在其他方法中,首先根据
indexPath.section