开发者

Delegate methods of UITableView are not responding

I am quite new to IOS development, so forgive me if this problem turns out to be trivial. The following is the case:

I want to be able to call the didSelectRowAtIndexPath method to trigger some action when a user interacts with a table view cell. However, it seems that this method is never called for some reason. I am fairly certain that my code is valid. I set the delegate and dataSource properties and I conform to the UITableViewDelegate and UITableViewDataSource protocols.

If someone could shed some light on this, I would be very grateful.

My code:

#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

and my .m file

#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 *)tabl开发者_开发问答eView:(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


Check the doc for tableView:willSelectRowAtIndexPath::

Return an NSIndexPath object other than indexPath if you want another cell to be selected. Return nil if you don't want the row selected.

You are returning nil, basically saying the table view that you don't want that row selected. Therefore tableView:didSelectRowAtIndexPath: is not called.


When I had this problem I found the solution was to use willSelectRowAtIndexPath. In other places in the same project didSelectItemAtIndexPath is getting called fine.

#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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜