UITableView的委托方法没有响应

时间:2011-03-16 12:53:51

标签: iphone ios uitableview delegates

我对IOS开发很新,所以请原谅我,如果这个问题变得微不足道。以下是这种情况:

我希望能够在用户与表格视图单元格交互时调用didSelectRowAtIndexPath方法来触发某些操作。但是,似乎从来没有因为某种原因调用此方法。我相当确定我的代码是有效的。我设置了delegatedataSource属性,并且符合UITableViewDelegateUITableViewDataSource协议。

如果有人能够对此有所了解,我将非常感激。

我的代码:

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@class Employee;

#define kTelephoneRowIndex  0
#define kEmailRowIndex      1
#define kLeftLabelTag        4096
#define kRightLabelTag      4097


@interface PersonnelDetailsViewController : UIViewController <UITableViewDelegate,  
UITableViewDataSource, MFMailComposeViewControllerDelegate> {
    UITableView *contactDetailsTable;
Employee *employee;
}

@property (nonatomic, retain) UITableView *contactDetailsTable;
@property (nonatomic, retain) Employee *employee;

+ (NSString *)resourceFilePath:(NSString *)fileName ofType:(NSString *)type;


@end

我的.m文件

#import <QuartzCore/QuartzCore.h>
#import "PersonnelDetailsViewController.h"
#import "Employee.h"


@implementation PersonnelDetailsViewController

@synthesize contactDetailsTable, employee;


+ (NSString *)resourceFilePath:(NSString *)fileName ofType:(NSString *)type {
return [[NSBundle mainBundle] pathForResource:fileName ofType:type];
}

- (void)loadView {
[super loadView];

// Create the header view for the table view
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 110)];

// Set the image, using rounded corners
UIImage *employeeImage;
if ([employee.image length] == 0)
    employeeImage = [UIImage imageNamed:@"logoL1nda70by80"];
else
    employeeImage = [UIImage imageNamed:employee.image];

UIImageView *employeeImageView = [[UIImageView alloc] initWithImage:employeeImage];
employeeImageView.frame = CGRectMake(10, 15, 70, 80);
employeeImageView.layer.masksToBounds = YES;
employeeImageView.layer.cornerRadius = 5.0;
employeeImageView.layer.borderWidth = 1.0;
employeeImageView.layer.borderColor = [[UIColor grayColor] CGColor];
[headerView addSubview:employeeImageView];

UILabel *nameView = [[UILabel alloc] initWithFrame:CGRectMake(95, 35, 180, 20)];
nameView.text = employee.name;
nameView.font = [UIFont boldSystemFontOfSize:18];
[headerView addSubview:nameView];

UILabel *functionView = [[UILabel alloc] initWithFrame:CGRectMake(95, 55, 140, 16)];
functionView.text = employee.function;
functionView.textColor = [UIColor grayColor];
functionView.font = [UIFont systemFontOfSize:14];
[headerView addSubview:functionView];

[employeeImage release];
[employeeImageView release];
[nameView release];
[functionView release];

contactDetailsTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 370) style: UITableViewStyleGrouped];
contactDetailsTable.backgroundColor = [UIColor clearColor];
contactDetailsTable.tableHeaderView = headerView;
[headerView release];
contactDetailsTable.delegate = self;
contactDetailsTable.dataSource = self;

[self.view addSubview:contactDetailsTable];
}

- (void)viewDidUnload {
self.contactDetailsTable = nil;
self.employee = nil;

[super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated {
[contactDetailsTable reloadData];
}

- (void)dealloc {
[contactDetailsTable release];
[employee release];
[super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *contactDetailIdentifier = @"contactDetailIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:contactDetailIdentifier];

if (cell == nil) {      
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:contactDetailIdentifier] autorelease];
    UILabel *leftLabel = [[UITextField alloc] initWithFrame:CGRectMake(5, 12, 70, 25)];
    leftLabel.textAlignment = UITextAlignmentRight;
    leftLabel.textColor = [UIColor grayColor];
    leftLabel.tag = kLeftLabelTag;
    leftLabel.userInteractionEnabled = NO;
    [cell.contentView addSubview:leftLabel];
    [leftLabel release];

    UILabel *rightLabel = [[UITextField alloc] initWithFrame:CGRectMake(90, 12, 160, 25)];
    rightLabel.font = [UIFont boldSystemFontOfSize:16];
    rightLabel.tag = kRightLabelTag;
    rightLabel.userInteractionEnabled = NO;
    [cell.contentView addSubview:rightLabel];
    [rightLabel release];
}
NSUInteger row = [indexPath row];
UILabel *leftLabel = (UILabel *)[cell viewWithTag:kLeftLabelTag];
UILabel *rightLabel = (UILabel *)[cell viewWithTag:kRightLabelTag];

switch (row) {
    case kTelephoneRowIndex:
        leftLabel.text = @"telefoon";
        rightLabel.text = employee.phone;
        break;
    case kEmailRowIndex:
        leftLabel.text = @"e-mail";
        rightLabel.text = employee.email;
        break;
    default:
        break;
}

return cell;
}

#pragma mark -
#pragma mark Table View Delegate Methods
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = indexPath.row;

if (row == 1) {
    MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
    [mailController setToRecipients:[NSArray arrayWithObject:employee.email]];
    [mailController.navigationBar setTintColor:[UIColor blackColor]];

    [self presentModalViewController:mailController animated:YES];
    mailController.mailComposeDelegate = self;
    [mailController release];
}

NSLog(@"Row selected is: %d", row);
}

#pragma mark -
#pragma mark MFMailCompose View Delegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[self dismissModalViewControllerAnimated:YES];
}


@end

2 个答案:

答案 0 :(得分:1)

检查tableView:willSelectRowAtIndexPath:的文档:

  

如果要选择其他单元格,则返回indexPath以外的NSIndexPath对象。如果您不想选择该行,则返回nil。

您正在返回nil,基本上是说您不希望选择该行的表格视图。因此,tableView:didSelectRowAtIndexPath:不会被调用。

答案 1 :(得分:0)

当我遇到这个问题时,我发现解决方案是使用willSelectRowAtIndexPath。在同一个项目的其他地方,didSelectItemAtIndexPath被称为罚款。

#pragma mark - UITableViewDelegate

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // for some reason this is getting called instead of didSelectItemAtIndexPath
    [self viewDetails:indexPath.row];
    return indexPath;
}

- (void)tableView:(UITableView *)tableView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    [self viewDetails:indexPath.row];
}