从 UITableView 选定索引更新 detailDescriptionLabel 数据
Posted
技术标签:
【中文标题】从 UITableView 选定索引更新 detailDescriptionLabel 数据【英文标题】:Updating detailDescriptionLabel data from UITableView selected index 【发布时间】:2012-08-15 16:56:58 【问题描述】:我有一个用于 iPad 的拆分视图控制器,左侧有一个向下钻取表。我可以毫无问题地使用向下钻取填充表格,但我似乎无法让我在左侧的 UITableView 中单击的项目显示在右侧的 detailDescriptionLabel 中。
我的 ProductViewController.m 中有以下代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"Row clicked: %i", indexPath.row);
if (_delegate != nil)
Product *product = (Product *) [_products objectAtIndex:indexPath.row];
[_delegate productSelectionChanged:product];
DetailedVC *detailView = [[DetailedVC alloc] initWithNibName:@"DetailedVC" bundle:[NSBundle mainBundle]];
NSLog(@"User clicked on: %@", [NSString stringWithFormat:@"%d",indexPath.row]);
detailView.detailDescriptionLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
[self.navigationController pushViewController:detailView animated:YES];
这里发生的事情是我将DetailedVC 推到我的表格视图所在的左侧,我真正想要做的是在右侧的详细视图中看到单击的行已更新。我可以在日志窗口中看到我点击了某个索引,因此我知道我正在捕获点击事件并获取一个值。
在我的DetailedVC.m 中,我有以下代码来更新此标签。
- (void)viewDidLoad
[super viewDidLoad];
[self configureView];
- (void) configureView
if (self.detailItem)
self.detailDescriptionLabel.text = [self.detailItem description];
NSLog(@"Item: %@", [self.detailItem description]);
如果我编辑 viewDidLoad,我会得到 [self.detailItem description] 的 (null)
- (void)viewDidLoad
[super viewDidLoad];
NSLog(@"Detail description label: %@", [self.detailItem description]);
ProductViewController.h
@interface ProductViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
NSMutableArray *_products;
UITableView *_productsTableView;
id<ProductSelectionDelegate> _delegate;
@property (nonatomic, strong) IBOutlet UITableView *productsTableView;
@property (nonatomic, retain) NSMutableArray *products;
@property (nonatomic, assign) id<ProductSelectionDelegate> delegate;
@end
AppDelegate.h
@class ProductViewController;
@class DetailedVC;
@interface TabAndSplitAppAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
UIWindow *window;
UITabBarController *tabBarController;
ProductViewController *_productViewController;
DetailedVC *_detailedViewController;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet ProductViewController *productViewController;
@property (nonatomic, retain) IBOutlet DetailedVC *detailedViewController;
@end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
NSMutableArray *products = [NSMutableArray array];
[products addObject:[[Product alloc] initWithName:@"Product 1 Name" desc:@"Product 1 Description"]];
_detailedViewController.product = [products objectAtIndex:0];
// Override point for customization after app launch.
//create split view controller
RootVC *rvc=[[RootVC alloc] init];
rvc.title=@"Root VC";
DetailedVC *dvc=[[DetailedVC alloc] init];
dvc.title=@"Detailed VC";
_productViewController.delegate = _detailedViewController;
MySplitViewController *msc = [[MySplitViewController alloc] initwithLeftVC:rvc rightVC:dvc];
msc.title=@"First";
//create a temporary VC to show in second tab
SecondViewController *vc2 = [[SecondViewController alloc] init];
vc2.title=@"Second";
//make an array containing these two view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:msc,vc2,nil];
[tabBarController setViewControllers:viewControllers];
tabBarController.view.backgroundColor=[UIColor blackColor];
for(UITabBarItem*t in tabBarController.tabBar.items)
t.image=[UIImage imageNamed:@"icon.png"];
t.badgeValue=[NSString stringWithFormat:@"%d",([tabBarController.tabBar.items indexOfObject:t]+1)];
//the views are retained their new owners, so we can release
[rvc release];
[dvc release];
[msc release];
[vc2 release];
// Add the tab bar controller's current view as a subview of the window
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
MySplitViewController.h
#import <UIKit/UIKit.h>
@interface MySplitViewController : UIViewController
UINavigationController *leftController;
UINavigationController *rightController;
@property (nonatomic, retain) UINavigationController *leftController;
@property (nonatomic, retain) UINavigationController *rightController;
- (void)layoutViews:(UIInterfaceOrientation)orientation initialVerticalOffset:(float)offset;
- (MySplitViewController*) initwithLeftVC:(UIViewController*)leftvc rightVC:(UIViewController*)rightvc;
@end
MySplitViewController.m
- (MySplitViewController*) initwithLeftVC:(UIViewController*)leftvc rightVC:(UIViewController*)rightvc
if(self=[super init])
UINavigationController *lnc=[[UINavigationController alloc] initWithRootViewController:leftvc];
lnc.navigationBarHidden=NO;
self.leftController=lnc;
[lnc release];
UINavigationController *rnc=[[UINavigationController alloc] initWithRootViewController:rightvc];
rnc.navigationBarHidden=NO;
self.rightController=rnc;
[rnc release];
return self;
产品.h
#import <Foundation/Foundation.h>
@interface Product : NSObject
NSString *_productID;
NSString *_productDescription;
@property (nonatomic, copy) NSString *productID;
@property (nonatomic, copy) NSString *productDescription;
- (Product *)initWithName:(NSString *)productID desc:(NSString *)productDescription;
@end
产品.m
#import "Product.h"
@implementation Product
@synthesize productID = _productID;
@synthesize productDescription = _productDescription;
- (Product *)initWithName:(NSString *)productID desc:(NSString *)productDescription
if ((self = [super init]))
self.productID = productID;
self.productDescription = productDescription;
return self;
@end
【问题讨论】:
您是否从configureView
获得NSLog 输出?我看不到你在任何地方设置detailItem
。
No Jorn,self.detailItem 没有填充DetailedVC.m 中的值,因此if... 中的NSLog 永远不会执行。我的印象是这个值是在 ProductViewController.m 的 didSelectRowAtIndex 方法中设置的
【参考方案1】:
好的,请在 ProductViewController 中试试这个:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"Row clicked: %i", indexPath.row);
Product *product = (Product *) [_products objectAtIndex:indexPath.row];
NSLog(@"Product: %@", product);
TabAndSplitAppAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSLog(@"appDelegate: %@", appDelegate);
UITabBarController *tbc = appDelegate.tabBarController;
NSLog(@"tbc: %@", tbc);
MySplitViewController *msvc = tbc.selectedViewController;
NSLog(@"msvc: %@", msvc);
UINavigationController *rnc = msvc.rightController;
NSLog(@"rnc: %@", rnc);
DetailedVC *dvc = rnc.topViewController;
NSLog(@"dvc: %@", dvc);
dvc.detailDescriptionLabel.text = @"We found our DetailedVC";
[dvc productSelectionChanged:product];
此代码不是您应该如何正确构建程序的示例。这只是为了帮助我(和你)理解你的视图控制器层次结构。
在选择产品时通知代表的最初想法是正确的,但在您的情况下它不起作用,因为对象没有正确连接。不过,您应该尝试这样做。在您发布的代码中,我没有看到 ProductViewController
和 DetailedVC
都直接可见的位置,因此您可以说
productViewControllerInstance.delegate = detailedVCinstance;
最接近的是在 AppDelegate 中,您拥有 RootVC
实例 rvc
,它可能最终会创建 ProductViewController
和 dvc
。也许您可以将dvc
提供给rvc
,以便在创建时将其设置为ProductViewController
的委托。祝你好运!
【讨论】:
好吧,乔恩,我现在看到 [self.detailItem 描述] 显示了我在 configureView 方法中单击的行的值: NSLog(@"Item: %@", [self.detailItem描述]);这是一个很大的帮助,但我仍然没有看到该视图中的实际标签显示任何价值。我有一个名为 detailDescriptionLabel 的 UILabel 作为我的DetailedVC.h 文件中的一个属性,并且我已将它连接到界面构建器文件DetailedVC.xib 中的实际UILabel - 此标签在UI 中有一个名为“Label”的占位符值。运行应用看不到占位符值、“标签”或更新后的值。 嗯,也许连接您的detailDescriptionLabel
插座有问题。将NSLog(@"Label: %@", self.detailDescriptionLabel);
添加到configureView
会得到什么?
我明白了.... 标签:ProductViewController
的_delegate 是哪个类的实例以及它的productSelectionChanged:
方法看起来如何?它被调用了吗?
好的,我将我的 ProductViewController.h 文件添加到上面的线程中。我正在使用 ProductViewController : UIViewController 以上是关于从 UITableView 选定索引更新 detailDescriptionLabel 数据的主要内容,如果未能解决你的问题,请参考以下文章
如何从数组中过滤多个选定值并将其从 uibutton 加载到其他 uitableview
试图从 UITableView 返回选定的单元格,但它只返回 nil