English 中文(简体)
电话礼宾问题
原标题:iphone protocol problem

我在这里找不到一件事,在那里,我有一个班子,即EditableCell和礼宾,EditableCellDelegate,被定义为处理一个编辑桌子。 (原始代码来自Paul Deitel的“iPhone for Programmers”。)

虽然我把EditableCell的头盔进口到我的档案客户EditTVC.h,但EditableCell的特性和方法在用户EditTVC.m中并不得到承认。

这里是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

而且

#import "EditableCell.h"
@implementation EditableCell

@synthesize textField; // synthesize get 以及 set methods for delegate
@synthesize label; // synthesize get 以及 set methods for delegate
@synthesize delegate; // synthesize get 以及 set methods for delegate

// initialize the cell
- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier
{
   // call the superclass
   if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
   {
      // create the label on the left side
      label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 0, 20)];

      // create the text field to the right of the label
      textField =
         [[UITextField alloc] initWithFrame:CGRectMake(0, 10, 0, 20)];

      [textField setDelegate:self]; // set the delegate to this object

      // call textFieldDidEndOnExit when the Done key is touched
      [textField addTarget:self action:@selector(textFieldDidEndOnExit)
          forControlEvents:UIControlEventEditingDidEndOnExit];
      [self.contentView addSubview:label]; // add label to the cell
      [self.contentView addSubview:textField]; // add textField to cell
   } // end if

   return self; // return this Editable cell
} // end method initWithFrame:reuseIdentifier:

// method is called when the user touches the Done button on the keyboard
- (void)textFieldDidEndOnExit
{
   [textField resignFirstResponder]; // make the keyboard go away
   [delegate editableCellDidEndOnExit:self]; // call the delegate method
} // end method textFieldDidEndOnExit

// set the text of the label
- (void)setLabelText:(NSString *)text
{
   label.text = text; // update the text

   // get the size of the passed text with the current font
   CGSize size = [text sizeWithFont:label.font];
   CGRect labelFrame = label.frame; // get the frame of the label
   labelFrame.size.width = size.width; // size the frame to fit the text
   label.frame = labelFrame; // update the label with the new frame

   CGRect textFieldFrame = textField.frame; // get the frame of textField

   // move textField to 30 pts to the right of label
   textFieldFrame.origin.x = size.width + 30;

   // set the width to fill the remainder of the screen
   textFieldFrame.size.width =
   self.frame.size.width - textFieldFrame.origin.x;
   textField.frame = textFieldFrame; // assign the new frame
} // end method setLabelText:

// clear the text in textField
- (void)clearText
{
   textField.text = @""; // update textField with an empty string
} // end method clearText

// delegate method of UITextField, called when a text field begins editing
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
   [delegate editableCellDidBeginEditing:self]; // inform the delegate
} // end method textFieldDidBeginEditing:

// delegate method of UITextField, called when a text field ends editing
- (void)textFieldDidEndEditing:(UITextField *)textField
{
   [delegate editableCellDidEndEditing:self]; // inform the delegate
} // end method textFieldDidEndEditing:

// free EditableCell s memory
- (void)dealloc
{
  [textField release]; // release the textField UITextField
  [label release]; // release the label UILabel
  [super dealloc]; // call the superclass s dealloc method
} // end method dealloc
@end // end EditableCell class definition

而且here is the relevant code from ClientEditTVC.h 以及 .m

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


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


@end

以及

#import "ClientEditTVC.h"


@implementation ClientEditTVC

// stuff here

- (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

    // update the text in the text field with the value
    //cell.textField.text = [data valueForKey:key];


    return cell;
}

// more stuff here

@end

我在上接到警告:[Mcell setLabelText:key]; 电池不能对可铺设Text做出反应。 但是,在EditableCell,正在用断点进行追踪。

cell.text Field.text的行文(摘要)产生错误,财产文本 未找到的物体

Obviously the compiler is not seeing that I have subclassed UITableViewCell, 以及 I m not sure why. It s even stranger, to me, that the setLableText method is getting executed. I went back to the sample code provided by Deitel, 以及 these problems don t occur. I have looked my code over carefully, 以及 can t seen any significant difference.

我对我所期待的建议表示赞赏。

最佳回答

页: 1

UITableViewCell *cell;

因此,它不是一个<条码>。 请宣布:

EditableCell *cell;

它不会给你警告。 也许会警告,你重新指定一个<代码>可调用的电文/代码>至<代码>cell,但你可以“固定”使用一种电文,即。

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

Also, you need to adjust the return type of the function. This should eliminate all compiler warnings.

问题回答

Redeclare cell/code> as a EditableCell * und a UITableViewCell *, 警告应当撤销。

- (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;
}




相关问题
Create binary data using Ruby?

i was palying with the ruby sockets, so i ended up trying to put an IP packet togather, then i took an ip packet and try to make a new one just like it. now my problem is: if the packet is: 45 00 00 ...

How to associate a URL to an application using Cocoa

From the command prompt it s possible to run for example "open http://www.example.com" and have that homepage displayed in the default browser. Similarly you can run "open callto:xyz123" to open up ...

in protocol with regard to sequence

How is this implemented at a python level? I ve got an object that pretends to be a dict for the most part (in retrospect I should have just subclassed dict, but I d rather not refactor the codebase, ...

Can anyone guess what protocol these packets belong to?

We see these packets being injected in an FTP-DTP channel during a downlink file transfer on Telstra s NEXTG mobile network. We are not sure if these are network level packets, a problem with our 3G ...

How to release attribute which is protocol?

I have working for iPhone development for a while. First time, I so surprised with memory-management in objective-c :). but now I got it a little bit. The question is, sometime, I use protocol as an ...

Reverse-engineering of communication protocols

Just curious - what are some automatic or even semi-automatic techniques for reverse-engineering of communication protocols? I am particularly interested in the case when one s sniffing traffic and ...

The most efficient DHT

What is the most efficient DHT? I am looking for name and/or some kind of implementation or related work, but I am not looking for the one that is most used. Efficient in terms of CPU execution ...