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

时间:2014-04-17 04:19:46

标签: ios uitableview

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

@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个对象是字母表中每个字母的数组,然后是包含啤酒名称的数组。基本上我想检索用户选择的啤酒名称。

1 个答案:

答案 0 :(得分:0)

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

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

但在你的didDeselectRowAtIndexPath中你只是这样做:

_testLabel.text = keys[indexPath.row];

你不应该在didDeselectRowAtIndexPath

中这样做
//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];
}

希望这会有所帮助.. :)