0

I have experience with Java and Android development, and am now attempting to learn Objective-C and iPhone/iPad development. In order to help teach myself, I am re-writing an application I made for android over to iPhone.

I am trying to create a tableView, that will contain names of members, and then have contact information in a different view when pressed. Specifically, I am having trouble setting my cell label from my NSMutableArray "currentMembers", which is filled with NSObjects "member" that has five properties, being NSStrings. Two of the variables include a first name and a last name. I would like to set the cell label to be a full name, so a concatenated string of firstName + lastName. The following is some of the code from my tableview class.

**Just to clarify, my array is indeed filled with objects which are properly defined and not 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
2
  • What do you mean by "These "member" objects are made up of five NSString variables."? You mean you are using NSMutableArray with NSMutableDictionary? Commented Jun 25, 2013 at 22:04
  • sorry for not clarifying, I have an NSMutableArray "currentMembers", filled with NSObjects "member" that has five properties, being NSStrings. I'll update my description. Commented Jun 25, 2013 at 22:12

1 Answer 1

1

The logic you're talking about should be in cellForRowAtIndexPath: and should be along the lines of:

AKPsiMember *member = [self.currentMembers objectAtIndex:indexPath.row];
NSString *fullName = [NSString stringWithFormat:@"%@ %@", member.firstName, member.lastName];

cell.textLabel.text = fullName;

If first name or last name can ever be nil the above won't produce a pretty full name string...


objectAtIndex: isn't really related to the cell. Its an array method to get the value at a specified index. The index, in this case, is being specified as indexPath.row which is simply the current row of the table (provided as an index path because there may also be a relevant section).

You don't appear to be using any prototype cells. You're just reusing cell instances. The table view manages this for you, all you need to do is try getting an instance from the pool (using dequeueReusableCellWithIdentifier:) before you create one yourself.

Sign up to request clarification or add additional context in comments.

1 Comment

Problem solved! Coming from some knowledge in android/java, that prototype cell/cellForRowAtIndexPath is really hard for me to wrap my head around. Any chance you could do me one last favor and explain the logic behind objectAtIndex:indexPath.row?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.