我试图将我创建的对象的NSMutableArray
显示为UITableView
。除了当我尝试滚动表格视图然后得到EXC_BAD_ACCESS时,所有内容都会显示。滚动表格视图时崩溃
这是我的目标:
.h文件中
@interface EmailAddressClass : NSObject {
@public NSString * fullName;
@public NSString * myEmailAddress;
BOOL isSelected;
}
@property (nonatomic, retain) NSString * fullName;
@property (nonatomic, retain) NSString * myEmailAddress;
- (void) setFullname:(NSString *)pMyName;
- (void) setMyEmailAddress:(NSString *)pEmailAddress;
- (void) setIsSelected:(BOOL)pSelectedFlag;
- (BOOL) getIsSelected;
- (NSString *) getFullname;
- (NSString *)getMyEmailAddress;
@end
.m文件
#import "EmailAddressClass.h"
@implementation EmailAddressClass
@synthesize fullName;
@synthesize myEmailAddress;
- (void) setFullname:(NSString *)pMyName{
fullName = pMyName;
}
- (void) setMyEmailAddress:(NSString *)pEmailAddress{
myEmailAddress = pEmailAddress;
}
- (NSString *) getFullname{
return fullName;
}
- (NSString *)getMyEmailAddress{
return myEmailAddress;
}
- (void) setIsSelected:(BOOL)pSelectedFlag{
isSelected = pSelectedFlag;
}
- (BOOL) getIsSelected{
return isSelected;
}
@end
直截了当不够;只是一个名字和电子邮件地址为NSString
s的小班。
这是我如何填充从地址簿姓名和电子邮件地址
- (NSMutableArray*)getAllEmailAddress{
CFMutableArrayRef myMutablePeople = [self getSortedAddressBook];
NSMutableArray *allEmails = [[NSMutableArray alloc] initWithCapacity:CFArrayGetCount(myMutablePeople)];
for (CFIndex i = 0; i < CFArrayGetCount(myMutablePeople); i++) {
ABRecordRef person = CFArrayGetValueAtIndex(myMutablePeople, i);
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (NSString *) ABRecordCopyValue(person, kABPersonLastNameProperty);
for (CFIndex j=0; j < ABMultiValueGetCount(emails); j++) {
NSString* email = (NSString*)ABMultiValueCopyValueAtIndex(emails, j);
EmailAddressClass * anEmailPerson = [EmailAddressClass alloc];
[anEmailPerson setIsSelected:YES];
[anEmailPerson setMyEmailAddress:email];
if ((firstName) && (lastName) != nil){
[anEmailPerson setFullname:[NSString stringWithFormat:@"%@ %@", firstName, lastName]];
} else if ((firstName != nil) && (lastName == nil)){
[anEmailPerson setFullname:[NSString stringWithFormat:@"%@", firstName]];
} else if ((firstName ==nil) && (lastName != nil)){
[anEmailPerson setFullname:[NSString stringWithFormat:@"%@", lastName]];
} else {
[anEmailPerson setFullname:[NSString stringWithFormat:@"%@", email]];
}
[allEmails addObject:anEmailPerson];
[email release];
[anEmailPerson release];
}
CFRelease(emails);
}
CFRelease(myMutablePeople);
// CFRelease(people);
return allEmails;
}
一切工作至此阵列;该数组将返回正确的人数及其电子邮件地址。
这是我如何填充表视图
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
thisPerson = (EmailAddressClass *)[allEmails objectAtIndex:indexPath.row];
cell.textLabel.text = [thisPerson getFullname];
return cell;
}
我缺少什么?
snipped of of EmailListViewController.h
#import "EmailAddressClass.h"
@interface EmailListViewController : UITableViewController {
NSMutableArray *allEmails;
EmailAddressClass * thisPerson;
}
@property (nonatomic, retain) NSMutableArray *allEmails;
@property (nonatomic, retain) EmailAddressClass * thisPerson;
@synthesize allEmails,thisPerson;既得到在EmailListViewController.m上viewDidLoad中.m文件
合成
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.toolbarHidden=NO;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
self.tableView.rowHeight = ROW_HEIGHT;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
allEmails = [[MsgController sharedMsgController] getAllEmailAddress];
/*NSLog(@"There are : %d number of emails", [allEmails count]);
for (EmailAddressClass *email in allEmails)
{
NSLog(@"%@ : %@", [email getFullname],[email getMyEmailAddress]) ;
}
*/
}
控制台输出显示阵列
如果你做'allEmails'的属性? –
tableView:cellForRowAtIndexPath正在读取allEmails iVar或prop - 如何设置?谁和什么调用getAllEmailAddress并设置它? – bryanmac
allEmails是负责填充UItableview的类的一个属性。 getAllEmailAddress在ViewdidLoad上调用并分配给所有电子邮件 – skeo