代表没有从课堂上设置

时间:2013-11-27 04:49:15

标签: ios delegates protocols

  

TableViewRowProtocol

#import <Foundation/Foundation.h>

@protocol TableViewRowProtocol <NSObject>

-(void) addRow;

@end

在HeaderCell类中创建了一个委托,并从同一个

调用协议方法
  

HeaderCell.h

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

@interface HeaderCell : UITableViewCell<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UILabel* header;

@property (nonatomic,assign) id<TableViewRowProtocol> rowdelegate;

-(IBAction)add:(id)sender;
@end
  

HeaderCell.m

#import "HeaderCell.h"
#import "MembershipsView.h"

@implementation HeaderCell
@synthesize header,rowdelegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  {
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
      if (self) {
          // Initialization code
      }
      return self;
  }

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
  {
      [super setSelected:selected animated:animated];

      // Configure the view for the selected state
  }

  -(IBAction)add:(id)sender
   {
       [rowdelegate addRow];
   }
@end

然后我在类HeaderCell中创建了一个类MembershipsView的对象,并将rowdelegate设置为self,因为协议中的方法addrow在同一个类中实现

  

MembershipsView.h

#import <UIKit/UIKit.h>
#import "DoctorService.h"
#import "DEcell.h"
#import "HeaderCell.h"


@interface MembershipsView : UIView <UITableViewDataSource,UITableViewDataSource,UITextFieldDelegate,UIAlertViewDelegate,TableViewRowProtocol>

{
    NSMutableArray* memberships;

}
@property (nonatomic, assign) int edit;
@property (nonatomic, strong) IBOutlet UITableView* membershipsTable;
@property(nonatomic,strong) NSString* DoctorMembershipsDTO;
@property (nonatomic,strong) HeaderCell* ObjHeaderCell;
@property(nonatomic, assign) int selectedDoctorId;

-(void)whenViewLoaded;

@end
  

MembershipsView.m

#import "MembershipsView.h"

@implementation MembershipsView
@synthesize selectedDoctorId,DoctorMembershipsDTO,edit,ObjHeaderCell;

- (id)initWithFrame:(CGRect)frame
{
    self = [[[NSBundle mainBundle] loadNibNamed:@"MembershipsView" owner:self options:Nil] lastObject];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)whenViewLoaded
{
    DoctorMembershipsDTO=[[NSString alloc]init];
    memberships=[[DoctorService sharedInstance] getMembershipsByDoctorId:selectedDoctorId];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return memberships.count+1;

}


-(void)addRow
{
    UIAlertView* addMembership = [[UIAlertView alloc]initWithTitle:@"Add Membership"` `message:@" " delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];

   addMembership.alertViewStyle = UIAlertViewStylePlainTextInput;
    [addMembership textFieldAtIndex:0].delegate = self;
    [addMembership show];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if(edit == 1)
    {
        if (0 == indexPath.row)
        {

            static NSString *cellId = @"HeaderCell";

            ObjHeaderCell = (HeaderCell *)[tableView dequeueReusableCellWithIdentifier:cellId];

            ObjHeaderCell.rowdelegate=self;
            if (ObjHeaderCell == nil)
            {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HeaderCell"   owner:self options:nil];
                `ObjHeaderCell = [nib objectAtIndex:0];
            }

            ObjHeaderCell.header.text = @"Memberships";
            return ObjHeaderCell;
        }
        static NSString *cellId = @"Cell";

        DEcell *cell = (DEcell *)[tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DEcell"   owner:self options:nil];
            `cell = [nib objectAtIndex:0];
        }

        DoctorMembershipsDTO = [memberships objectAtIndex:indexPath.row-1];

        cell.labelText.text = DoctorMembershipsDTO;
        return cell;

    }
    else
    {
        static NSString* CellId= @"cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId] ;

        }

        DoctorMembershipsDTO = [memberships objectAtIndex:indexPath.row];

        cell.textLabel.text = DoctorMembershipsDTO;
        return cell;
    }
}

@end

但是当我在MembershipsView类中创建HeaderCell的对象时,设置了rowsle的Gatedelegate =对象。然后方法被调用 请帮助,提前谢谢

1 个答案:

答案 0 :(得分:0)

初始化HeaderCell后尝试分配委托。

ObjHeaderCell = (HeaderCell *)[tableView dequeueReusableCellWithIdentifier:cellId];
if (ObjHeaderCell == nil)
{
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HeaderCell"   owner:self options:nil];
     ObjHeaderCell = [nib objectAtIndex:0];
}
ObjHeaderCell.rowdelegate=self;