gpt4 book ai didi

iphone - UITableView 的委托(delegate)方法没有响应

转载 作者:行者123 更新时间:2023-11-29 05:05:51 24 4
gpt4 key购买 nike

我对 IOS 开发还很陌生,所以如果这个问题是微不足道的,请原谅我。情况如下:

我希望能够在用户与 TableView 单元格交互时调用 didSelectRowAtIndexPath 方法来触发某些操作。然而,由于某种原因,这个方法似乎从未被调用过。我相当确定我的代码是有效的。我设置了 delegatedataSource 属性,并且遵守 UITableViewDelegateUITableViewDataSource 协议(protocol)。

如果有人能阐明这一点,我将非常感激。

我的代码:

#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

最佳答案

检查文档中的 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.

您返回nil,基本上是说 TableView 您不希望选择该行。因此 tableView:didSelectRowAtIndexPath: 不会被调用。

关于iphone - UITableView 的委托(delegate)方法没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5325534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com