2

我无法在我的 tableview 控制器实现中使用我在 tableviewcell 文件中实现的方法之一。我尝试搜索网络和 xcode 帮助,但没有成功。我的代码如下所示:

表视图控制器.h:

    #import TableViewCell.h

    @interface TableViewController : UITableViewController


    @property (nonatomic, strong) IBOutlet UIBarButtonItem *A1Buy;
    @property (nonatomic, getter = isUserInteractionEnabled) BOOL userInteractionEnabled;

    - (IBAction)A1Buy:(UIBarButtonItem *)sender;

表视图控制器.m:

    @implementation A1ViewController

    @synthesize A1Buy = _A1Buy;
    @synthesize userInteractionEnabled;

    - (IBAction)A1Buy:(UIBarButtonItem *)sender {
   [TableViewCell Enable]; //this is where it gives an error

    }

TableViewCell.h:

    @interface TableViewCell : UITableViewCell {
    BOOL Enable;
    BOOL Disable;
    }
    @property (nonatomic, getter = isUserInteractionEnabled) BOOL userInteractionEnabled;

TableViewCell.m:

    @implementation TableViewCell;

    @synthesize userInteractionEnabled;

    - (BOOL) Enable {
    return userInteractionEnabled = YES;
    }
    - (BOOL) Disable {
    return userInteractionEnabled = NO;
    }

如您所见,我正在尝试启用用户与按钮的交互,但 Xcode 只会给我诸如“类没有此方法”之类的错误。所有文件都正确导入,所以这不是原因。将不胜感激任何帮助。谢谢!

4

2 回答 2

1

首先,根据 Cocoa 标准命名你的方法和变量——类的首字母大写,变量和方法的首字母小写。

这样做应该很明显,您正在调用classEnable上的方法,它实际上是一个实例方法。您需要获取指向特定表格视图单元格的指针并调用该方法。TableViewCell

此外,您实施的方法非常令人困惑。为什么他们将分配的结果作为布尔值返回?这将始终返回 YES。您可能需要学习一些基本的 Objective-C 培训资源。

于 2012-11-18T17:41:22.403 回答
0

您被声明- (BOOL) Enable为实例方法。您不能使用类名调用实例方法。解决方案:

  1. 将方法声明为 Class 方法

     + (BOOL) Enable
    
  2. 创建类的对象,然后使用该对象调用方法

     TableViewCell *cellObj = [[TableViewCell alloc] init];
     [cellObj Enable];
    

请在此处阅读有关类方法的更多信息。

请参阅此处的 ios 编码约定。

于 2012-11-19T04:30:10.243 回答