简体   繁体   English

如何访问UITableView子视图选择?

[英]How to access UITableView subview selection?

I've created a UIViewController ( AnestDrugCalcViewController ) that has a button that opens a UITableViewController ( DrugTableViewController ) to select a drug. 我创建了一个UIViewControllerAnestDrugCalcViewController ),它有一个按钮,打开一个UITableViewControllerDrugTableViewController )来选择一种药物。 The DrugTableViewController is populated based on a NSMutableArray and is made up of several NSDictionary objects (drugName, drugDose, drugConcentration, etc.). DrugTableViewController基于NSMutableArray填充,由几个NSDictionary对象(drugName,drugDose,drugConcentration等)组成。

I'm able to open DrugTableViewController by pushing it onto the navigation stack, but I can't figure out how to tell from my AnestDrugCalcViewController what the properties of the selected item are. 我可以通过将它推入导航堆栈来打开DrugTableViewController ,但我无法弄清楚如何从我的AnestDrugCalcViewController告诉所选项目的属性是什么。

//  DrugTableViewController.h
#import <UIKit/UIKit.h>

@interface DrugTableViewController : UITableViewController {
NSMutableArray          *drugList;
NSIndexPath *lastIndexPath;
IBOutlet UITableView    *myTableView;
NSObject *selectedDrug;
}

@property (nonatomic, retain) NSArray *drugList;
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) NSObject *selectedDrug;

@end

Top of DrugTableViewController.m #import "DrugTableViewController.h" DrugTableViewController.m #import“DrugTableViewController.h”的顶部

@implementation DrugTableViewController

@synthesize drugList,myTableView, selectedDrug; 

You could have a property in vwDrugTable that stores the the selected row when the user selects it. 您可以在vwDrugTable中拥有一个属性, vwDrugTable在用户选择行时存储所选行。 Then, after that table view is removed and you are back in vwCalc , just get the data from that property. 然后,在删除该表视图并返回vwCalc ,只需从该属性中获取数据。

So that means in vwDrugTable , have something like this: 这意味着在vwDrugTable ,有这样的东西:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedDrug = [self.drugs objectAtIndex:indexPath.row];
}

Then just access selectedDrug from vwCalc when you need it. 然后在需要时从vwCalc访问selectedDrug

UITableView 's -indexPathForSelectedRow method will allow you to determine which row is selected: UITableView-indexPathForSelectedRow方法将允许您确定选择哪一行:

NSIndexPath *selectionPath = [vwDrugTable indexPathForSelectedRow];
if (index) {
    NSLog(@"Selected row:%u in section:%u", [selectionPath row], [selectionPath section]);
} else {
    NSLog(@"No selection");
}

To be notified when the selection changes, implement tableView:didSelectRowAtIndexPath: in your table view delegate: 要在选择更改时收到通知,请在表视图委托中实现tableView:didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Selection Changed");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM