Objective c iphone协议问题

Objective c iphone协议问题,objective-c,protocols,subclass,Objective C,Protocols,Subclass,这里我缺少了一些东西,我有一个类EditableCell和协议EditableCellDelegate,它们是为处理表单元格进行编辑而定义的。(原始代码来自Paul Deitel的“程序员iPhone”) 虽然我已经将EditableCell的头导入到我的文件ClientEditTVC.h中,但在ClientEditTVC.m中无法识别EditableCell的属性和方法 以下是EditableCell.h和.m的预写代码: #import <UIKit/UIKit.h> @pro

这里我缺少了一些东西,我有一个类EditableCell和协议EditableCellDelegate,它们是为处理表单元格进行编辑而定义的。(原始代码来自Paul Deitel的“程序员iPhone”)

虽然我已经将EditableCell的头导入到我的文件ClientEditTVC.h中,但在ClientEditTVC.m中无法识别EditableCell的属性和方法

以下是EditableCell.h和.m的预写代码:

#import <UIKit/UIKit.h>

@protocol EditableCellDelegate; // declare EditableCellDelegate Protocol

@interface EditableCell : UITableViewCell <UITextFieldDelegate>
{
   id <EditableCellDelegate> delegate; // this class's delegate
   UITextField *textField; // text field the user edits
   UILabel *label; // label on the left side of the cell
} // end instance variables declaration

// declare textField as a property
@property (nonatomic, retain) UITextField *textField;

// declare label as a property
@property (readonly, retain) UILabel *label;

//declare delegate as a property
@property (nonatomic, assign) id <EditableCellDelegate> delegate;

- (void)setLabelText:(NSString *)text; // set the text of label
- (void)clearText; // clear all the text out of textField
@end // end interface EditableCell

@protocol EditableCellDelegate // protocol for the delegate

// called when the user begins editing a cell
- (void)editableCellDidBeginEditing:(EditableCell *)cell;

// called when the user stops editing a cell
- (void)editableCellDidEndEditing:(EditableCell *)cell;

// called when the user touches the Done button on the keyboard
- (void)editableCellDidEndOnExit:(EditableCell *)cell;
@end // end protocol EditableCellDelegate
这是ClientEditTVC.h和.m的相关代码

#import <UIKit/UIKit.h>
#import "EditableCell.h"


@interface ClientEditTVC : UITableViewController <UITableViewDataSource, EditableCellDelegate> {
    NSArray *fields;
    NSMutableDictionary *data;
    BOOL keyboardShown;
    EditableCell *currentCell;
}


@end
我在[cell setLabelText:key]行得到警告UITableViewCell可能不会响应“setTableText”。但是通过断点跟踪,EditableCell中的setTextField代码正在执行

cell.textField.text的行(注释掉)产生错误,在类型为“UITableViewCell”的对象上找不到属性“textField”

显然,编译器没有看到我有子类UITableViewCell,我也不知道为什么。对我来说,更奇怪的是,setLableText方法正在被执行。我回到Deitel提供的示例代码,这些问题没有出现。我仔细查看了我的代码,没有发现任何显著的差异


如果您对我忽略的内容提出建议,我将不胜感激。

您这样声明了
cell

UITableViewCell *cell;
因此它不是一个可编辑的单元格。如果您这样声明:

EditableCell *cell;
它不会给你警告。它可能会警告您正在将
UITableViewCell
分配给
cell
,但您可以使用强制转换来“修复”,即

EditableCell *cell = (EditableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

此外,还需要调整函数的返回类型。这将消除所有编译器警告。

单元格
重新声明为
EditableCell*
而不是
UITableViewCell*
,并且该警告应该消失。

您尚未解释所做的任何更改。为什么要测试iskindof类:EditableCell?为什么不呢?谢谢,成功了。我很高兴事情这么简单。如果这种情况再次发生,我应该能够看到。还需要转换为EditableCell*。不过谢谢你。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[EditableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

// Configure the cell...
// get the key for the given index path
    NSString *key = [fields objectAtIndex:indexPath.row + indexPath.section * 3];
    [cell setLabelText:key]; // update the cell text with the key

    if ([cell isKindOfClass:[EditableCell class]]) {
        EditableCell* editableCell = (EditableCell*)cell;
// update the text in the text field with the value
        editableCell.textField.text = [data valueForKey:key];
    }
    else {
        assert(0 && "is it alright that this instance is not an EditableCell?");
    }

    return cell;
}
EditableCell *cell = (EditableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[EditableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

// Configure the cell...
// get the key for the given index path
    NSString *key = [fields objectAtIndex:indexPath.row + indexPath.section * 3];
    [cell setLabelText:key]; // update the cell text with the key

    if ([cell isKindOfClass:[EditableCell class]]) {
        EditableCell* editableCell = (EditableCell*)cell;
// update the text in the text field with the value
        editableCell.textField.text = [data valueForKey:key];
    }
    else {
        assert(0 && "is it alright that this instance is not an EditableCell?");
    }

    return cell;
}