我正在尝试向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
的小班。
这是我使用姓名和电子邮件地址从地址簿填充数组的方法
- (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;两者都在.m文件中合成
在EmailListViewController.m中的Viewdidload
- (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]) ;
}
*/
}
控制台输出显示数组中正确的名称和电子邮件
答案 0 :(得分:1)
当你进行循环和设置时,你可以通过调用stringWithFormat来创建字符串。由于那不是alloc,copy,mutableCopy,它是自动释放的。
然后你分配它,保留它但不保留它:
在循环中:
// this is autoreleased
[anEmailPerson setFullname:[NSString stringWithFormat:@"%@ %@", firstName, lastName]];
在课堂上:
// you're holding a reference to something that's about to go poof
- (void) setFullname:(NSString *)pMyName{
fullName = pMyName;
}
我建议保留它或将其声明为属性并合成它:
@property (retain, atomic) NSString *fullName;
然后在.m
@synthesize fullName;
我还建议您的getAllEmailAddresses函数执行自动释放 - 函数的调用者可以选择保留它。在您的函数自动释放后,让您的allEmails属性保留。内存管理规则说如果它不是以alloc,copy或immutableCopy开头的,那么它是自动释放的,并且由调用者保留。
阅读苹果内存管理指南 - 好读。
具体来说,在第二页上,阅读规则。