如何访问UITableView子视图选择?

时间:2022-09-06 20:25:02

I've created a UIViewController (AnestDrugCalcViewController) that has a button that opens a UITableViewController (DrugTableViewController) to select a drug. The DrugTableViewController is populated based on a NSMutableArray and is made up of several NSDictionary objects (drugName, drugDose, drugConcentration, etc.).

我创建了一个UIViewController (AnestDrugCalcViewController)它有一个按钮可以打开一个UITableViewController (DrugTableViewController)来选择药物。DrugTableViewController是基于一个NSMutableArray填充的,它是由几个NSDictionary对象(药名、药物、药物浓度等)组成的。

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。米#进口“DrugTableViewController.h”

@implementation DrugTableViewController

@synthesize drugList,myTableView, selectedDrug; 

2 个解决方案

#1


3  

You could have a property in vwDrugTable that stores the the selected row when the user selects it. Then, after that table view is removed and you are back in vwCalc, just get the data from that property.

您可以在vwDrugTable中拥有一个属性,在用户选择该行时存储所选行。然后,在删除表视图之后,您返回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。

#2


3  

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");
}

#1


3  

You could have a property in vwDrugTable that stores the the selected row when the user selects it. Then, after that table view is removed and you are back in vwCalc, just get the data from that property.

您可以在vwDrugTable中拥有一个属性,在用户选择该行时存储所选行。然后,在删除表视图之后,您返回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。

#2


3  

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");
}