gpt4 book ai didi

objective-c - 滚动表格 View 时崩溃

转载 作者:行者123 更新时间:2023-11-29 13:44:40 27 4
gpt4 key购买 nike

我正在尝试将我创建的对象的 NSMutableArray 显示到 UITableView。一切都会显示,除非我尝试滚动表格 View 然后我得到一个 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

足够简单;只是一个小类,将姓名和电子邮件地址保存为 NSStrings。

这就是我用姓名和电子邮件地址从地址簿填充数组的方式

- (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;
}

到目前为止一切正常;该数组返回正确的人数及其电子邮件地址。

这是我填充表格 View 的方式

- (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,这个人;两者都在 .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]) ;
}
*/
}

控制台输出在数组中显示正确的姓名和电子邮件

最佳答案

当您循环和设置时,您通过调用 stringWithFormat 创建字符串。因为那不是分配、复制、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 开头,则它是自动释放的,由调用者保留。

阅读 apple 内存管理指南 - 好读。

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

具体来说,在第二页,阅读规则。

关于objective-c - 滚动表格 View 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7897755/

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