2016-05-12 69 views
0

我想添加項目到這個文件中的籃子。我有自定義表格單元格,並將它與故事板中的IBOutlets鏈接起來。不知何故調試器到達deletegating對象內部的委託方法,但沒有進一步到TableVC,該方法符合,並且沒有看到日誌。TableViewCellDelegate不被稱爲

// SeatTableViewCell.h 
#import <UIKit/UIKit.h> 
#import "SeatTableViewCellDelegate.h" 

@interface SeatTableViewCell : UITableViewCell 
@property (nonatomic, assign) id<SeatTableViewCellDelegate> delegate; 
@property (nonatomic, weak) IBOutlet UILabel *rowLabel; 
@property (nonatomic, weak) IBOutlet UILabel *seatLabel; 
@property (nonatomic, weak) IBOutlet UILabel *priceLabel; 
@property (nonatomic, weak) IBOutlet UIImageView *addToBasketImageView; 
@end 

// SeatTableViewCell.m 
#import "SeatTableViewCell.h" 
@implementation SeatTableViewCell 
@synthesize rowLabel, seatLabel, priceLabel, addToBasketImageView; 
- (void)awakeFromNib { 
    [super awakeFromNib]; 

    UITapGestureRecognizer *singleImageTap = [[UITapGestureRecognizer alloc] initWithTarget: 
               self action:@selector(addToBasketTapDetected:)]; 
    singleImageTap.numberOfTapsRequired = 1; 
    [self.addToBasketImageView addGestureRecognizer:singleImageTap]; 
} 

- (void)addToBasketTapDetected: (UIGestureRecognizer *)sender { 
    //HERE the debugger stops, after step into nothing happens 
    [self.delegate tableViewCell:self didSelectImageView:self.addToBasketImageView]; 
} 
- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 
@end 
// SeatTableViewCellDelegate.h 
@protocol SeatTableViewCellDelegate <NSObject> 

- (void)tableViewCell:(UITableViewCell *)cell didSelectImageView:(UIImageView *)imageView; 

@end 

// ChooseSeatTableViewController.h 
#import <UIKit/UIKit.h> 
#import "SeatGroup.h" 
#import "SeatTableViewCellDelegate.h" 
@interface ChooseSeatTableViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, SeatTableViewCellDelegate> 
- (IBAction)onBackPressed:(id)sender; 
- (IBAction)OnChooseSectorClicked:(id)sender; 
-(void)reloadData:(SeatGroup *)selectedSeatGroup; 
@property (weak, nonatomic) IBOutlet UITableView *tableView; 
@property (nonatomic, strong) SeatGroup *seatGroup; 
@property (nonatomic, strong) NSString* perfUUID; 
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableHeightConstraint; 
@property (nonatomic, strong) NSMutableArray* seats; 
@end 

// ChooseSeatTableViewController.m 
#import "ChooseSeatTableViewController.h" 
#import "ConcertDescriptionViewController.h" 
#import "SeatTableViewCell.h"  
@interface ChooseSeatTableViewController() 
@property (nonatomic, strong) NSMutableArray *selectedIndexPathes; 
@property (strong) Seat* currentSeat; 
@end 

@implementation ChooseSeatTableViewController 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    int volume = self.seats.count; 
    return volume; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    _currentSeat = [_seats objectAtIndex:indexPath.row]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *identifier = @"seatCell"; 
    //todo try fix bug with dequing indexes 
    SeatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 

    Seat *cur = [_seats objectAtIndex:indexPath.row]; 
    _currentSeat = cur; 
    cell.rowLabel.text = cur.rowNum; 
    cell.seatLabel.text = cur.seatNum; 
    NSArray* piecesOfPrice = [cur.price componentsSeparatedByString: @","]; 
    if (piecesOfPrice.count > 1) { 
     cur.price = [piecesOfPrice objectAtIndex:0]; 
    } 
    NSMutableString *prm = [cur.price mutableCopy]; 
    [prm appendString:@" p"]; 
    cell.priceLabel.text = prm; 

     [cell.addToBasketImageView setUserInteractionEnabled:YES]; 

    if ([self.selectedIndexPathes containsObject:indexPath]) { 
     //set basket selected 
     NSString *thePath = [[NSBundle mainBundle] pathForResource:@"rectangle_21_copy_5" ofType:@"png"]; 
     UIImage *priceBckgImg = [[UIImage alloc] initWithContentsOfFile:thePath]; 
     cell.addToBasketImageView.image = priceBckgImg; 
    } else { 
     //set basket unselected 
     NSString *thePath = [[NSBundle mainBundle] pathForResource:@"rectangle_21_copy_6" ofType:@"png"]; 
     UIImage *priceBckgImg = [[UIImage alloc] initWithContentsOfFile:thePath]; 
     cell.addToBasketImageView.image = priceBckgImg; 
    } 

    return cell; 
} 

- (void)tableViewCell:(UITableViewCell *)cell didSelectImageView:(UIImageView *)imageView { 
    //HERE the debugger doesnt reach 
    NSLog(@"Hi, debugger in didSelectImageView"); 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
    if (![self.selectedIndexPathes containsObject:indexPath]) { 
     [self.selectedIndexPathes addObject:indexPath]; 
    } 
    NSString *thePath = [[NSBundle mainBundle] pathForResource:@"rectangle_21_copy_5" ofType:@"png"]; 
    UIImage *priceBckgImg = [[UIImage alloc] initWithContentsOfFile:thePath]; 
    [imageView setImage:priceBckgImg]; 
    [self.tableView reloadData]; 
} 

.. 爲什麼我的協議沒有被調用?

+1

確保您的委託是不是nil – Rajesh

+0

在調試器,我可以看到0,但不是零_delegate \t ID \t 0x0 \t 0x00000000 – AlexInspired

+0

這意味着你沒有對象。這是非常基本的問題。不鼓勵在這裏提出這些問題。 – Rajesh

回答

1

你必須設置你的視圖控制器爲委託:

1

AlexInspired,

唯一缺少的語句是的cellForRowAtIndexPath :)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *identifier = @"seatCell"; 
    //todo try fix bug with dequing indexes 
    SeatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 

    Seat *cur = [_seats objectAtIndex:indexPath.row]; 
    _currentSeat = cur; 
    cell.rowLabel.text = cur.rowNum; 
    cell.seatLabel.text = cur.seatNum; 
    cell.delegate = self; 
    .... 
    return cell; 
} 
1

您沒有爲設置委託tableview單元格。

設置代表和享受:)你的問題解決了。

cell.delegate = self 
1

你忘記設置代表自我。所以在cellForRowAtIndexPath組代表自我喜歡,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     NSString *identifier = @"seatCell"; 
//todo try fix bug with dequing indexes 
    SeatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 

     cell.delegate = self; 

} 

這個要指定當前類對象細胞類,所以你可以能夠調用從細胞類當前類的方法。

:)

中的cellForRowAtIndexPath方法
1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *identifier = @"seatCell"; 
    //todo try fix bug with dequing indexes 
    SeatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 
cell.delegate=self; 
. 
. 
. 
1

寫下面的代碼它會幫助你

NSString *cellDdentifier = @"seatCell"; 
SeatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 
cell.delegate = self