【发布时间】:2012-04-17 06:43:53
【问题描述】:
所以我有一个自定义 UITableViewCell,它包含对其包含视图控制器(其中包含其表的 VC)的引用。
// MyCell.h
#import <UIKit/UIKit.h>
#import "RootViewController.h"
@interface MyCell : UITableViewCell
@property (nonatomic, strong) IBOutlet RootViewController *rootViewController;
-(IBAction)checkBoxClicked:(UIButton*)sender;
// MyCell.m
@implementation MyCell
@synthesize rootViewController = _rootViewController;
-(IBAction)checkBoxClicked:(UIButton*)sender
{
[self setCheckBoxChecked:!_checkBoxChecked];
[_rootViewController refreshVisibleViewForCellTagged:self.tag];
}
在我的单元格中,我有一个按钮可以更改一个变量,然后在我的 rootViewController 中调用一个函数。但是,当我尝试访问 refreshVisibleViewForCellTagged 方法内的 RootViewController 中的任何对象时,实际上调用了该方法,它们是 '0x0' / nil;
// RootViewController.h
@interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) IBOutlet UITableView *myTableView;
// RootViewController.m
- (void) refreshVisibleViewForCellTagged:(NSInteger)cellTag
{
UITableView *tableView = self.myTableView; // nil
NSIndexPath *indexPath = [self.myTableView indexPathForSelectedRow]; // nil
MyCell *selectedCell = (MyCell*)[self.myTableView cellForRowAtIndexPath:indexPath]; // nil
if (selectedCell.tag == cellTag) {
NSLog(@"Refresh one way.");
} else {
NSLog(@"Do something else.");
}
}
谁能解释一下为什么我不能从“refreshVisibleViewForCellTagged”方法中访问 RootController 中的任何对象/变量?
请,谢谢!
** 我的大问题是为什么在从不同的视图控制器调用视图控制器中的方法时无法访问任何对象。这里有一些我不知道的重要编程事实,这是权限问题吗?在这种情况下,我没有使用@class(前向分类)。
【问题讨论】:
-
您不应该继承
UITableViewCellDelegate协议并通过委托模型生成反馈吗? -
@Brooks - 是的,我收到 cellTag 没有任何问题。
-
@trojanfoe - 我试图在根视图控制器中使用此方法执行的操作是查看当前选定的单元格(其具有可见的详细视图,因为 RootView 是显示 50% 表的分屏和50% 带标签的视图)因此,如果当前选定的单元格是单击复选框的单元格,则刷新视图中的标签以反映复选框的新值。我不知道 Delegate 方法如何帮助我完成这样的自定义任务。
-
它不会让它变得更容易,但它会让你的子类单元更可重用。
标签: iphone objective-c ios ipad