2011-10-26 114 views
1

我试图将我创建的对象的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]) ; 
    } 
    */ 
} 

控制台输出显示阵列

+0

如果你做'allEmails'的属性? –

+0

tableView:cellForRowAtIndexPath正在读取allEmails iVar或prop - 如何设置?谁和什么调用getAllEmailAddress并设置它? – bryanmac

+0

allEmails是负责填充UItableview的类的一个属性。 getAllEmailAddress在ViewdidLoad上调用并分配给所有电子邮件 – skeo

回答

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财产在你的函数autoreleases后保留。内存管理规则说,如果它不是以alloc,copy或immutableCopy开头的,它是自动释放的,取决于调用者。

阅读苹果内存管理指南 - 很好的阅读。

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

具体而言,在第二页上,读出的规则。

+0

谢谢你的提示。我宣布全名和合成it..and是解决我的问题,我敲我的头靠在墙上不知道为什么事情这么简单了崩溃...再次感谢 – skeo

+0

你可以投和/或接受答案,如果它有帮助?我看到你的新... – bryanmac

+0

还是相当新的......再次感谢 – skeo