私は Java と Android の開発の経験があり、現在、Objective-C と iPhone/iPad の開発を学ぼうとしています。独学を支援するために、Android 用に作成したアプリケーションを iPhone 用に書き直しています。
メンバーの名前を含むtableViewを作成しようとしていますが、押すと別のビューに連絡先情報が表示されます。具体的には、NSStrings である 5 つのプロパティを持つ NSObjects "member" で満たされた NSMutableArray "currentMembers" からセル ラベルを設定するのに問題があります。2 つの変数には、名と姓が含まれます。セルラベルをフルネームに設定したいので、firstName + lastName の連結文字列です。以下は、テーブルビュー クラスのコードの一部です。
**明確にするために、私の配列は実際にnilではなく適切に定義されたオブジェクトで満たされています
.m
@implementation CurrentMemberViewController
@synthesize currentMembers = _currentMembers;
@synthesize alumniMembers = _alumniMembers;
@synthesize currentMembersLoadCheck = _currentMembersLoadCheck;
- (void)viewDidLoad
{
[super viewDidLoad];
if(self.currentMembersLoadCheck == NO)
{
NSString *currentFilePath = [[NSBundle mainBundle] pathForResource:@"akpsi_contact_list" ofType:@"txt"];
// NSString *alumniFilePath = [[NSBundle mainBundle] pathForResource:@"akpsi_alumni_list" ofType:@"txt"];
[self readFileString:currentFilePath];
//[self readFileString:alumniFilePath];
self.currentMembersLoadCheck = YES;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.currentMembers count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Current Member Name";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//if you dont have string, make default cell
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [self.currentMembers objectAtIndex:indexPath.row];
//rest of implementation for creating cell label.. above is not correct
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
- (NSString *)loadFileToString:(NSString *)filePath
{
NSError *error = nil;
NSString *fileContent = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:&error];
if(error)
{
NSLog(@"ERROR while loading from file: %@", error);
}
return fileContent;
}
-(void)readFileString:(NSString *)filePath
{
NSScanner *scanner = [NSScanner scannerWithString: [self loadFileToString:filePath]];
//temporary variables
NSString *thisFirstName;
NSString *thisLastName;
NSString *thisPhoneNum;
NSString *thisEmail;
NSString *thisPledge;
NSString *thisMajor;
NSMutableArray *memberArray = [[NSMutableArray alloc]initWithCapacity:40];
//begin scanning string until end
while ([scanner isAtEnd] == NO)
{
//scan one line, set variables, begin at next line
NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
[scanner scanUpToCharactersFromSet:whitespace intoString:&thisFirstName];
[scanner scanCharactersFromSet:whitespace intoString:nil];
[scanner scanUpToCharactersFromSet:whitespace intoString:&thisLastName];
[scanner scanCharactersFromSet:whitespace intoString:nil];
[scanner scanUpToCharactersFromSet:whitespace intoString:&thisPhoneNum];
[scanner scanCharactersFromSet:whitespace intoString:nil];
[scanner scanUpToCharactersFromSet:whitespace intoString:&thisEmail];
[scanner scanCharactersFromSet:whitespace intoString:nil];
[scanner scanUpToCharactersFromSet:whitespace intoString:&thisPledge];
[scanner scanCharactersFromSet:whitespace intoString:nil];
NSCharacterSet *newLineCharacterSet = [NSCharacterSet newlineCharacterSet];
[scanner scanUpToCharactersFromSet:newLineCharacterSet intoString:&thisMajor];
[scanner scanCharactersFromSet:newLineCharacterSet intoString:nil];
//create member object
AKPsiMember *member = [[AKPsiMember alloc] init];
member.firstName = thisFirstName;
member.lastName = thisLastName;
member.phoneNum = thisPhoneNum;
member.emailAddress = thisEmail;
member.pledgeClass = thisPledge;
member.major = thisMajor;
//add to array depending on file being read
//broken?//
[memberArray addObject:member];
}
self.currentMembers = memberArray;
}
@end