我試圖將我創建的對象的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