Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7162971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:49:22+00:00 2026-05-28T13:49:22+00:00

I ran into a small problem. I created a UITableView where the user can

  • 0

I ran into a small problem. I created a UITableView where the user can add his contacts from his addressbook. The firstName gets displayed in the first row, the lastName in the second one. I know where the problem is, because right now I actually tell him how to do that.
But how can I tell him to display both names in the same cell? I did it with the following methods but they don’t work.

-(void)addObjectsFromArray:(NSArray *)otherArray
{
     NSMutableArray *myArray = [[NSMutableArray alloc] init];
     [myArray addObjectsFromArray:menuArray];
     NSLog(@"%@", myArray);
     [self.tableView reloadData];
}

The problem is that I cannot display objects from myArray because cellforRowAtIndex method does not get the value. And with that one I get the same result (firstName first Row, lastName second Row):

NSArray *objects = [[NSArray alloc] initWithObjects:firstName, lastName, nil];
NSString *cellValue = [objects objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
NSLog(@"%@", objects);

return cell;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:49:22+00:00Added an answer on May 28, 2026 at 1:49 pm

    Not only does your code display the first name and last name in different cells, it also does not work for rows at indexes greater than one. This is because your table data source reads from a brand-new array that you create with alloc/init on the first line, rather than reading from your model. Your little array contains only two objects, explaining the two rows.

    Here is what you need to do: first, make an array of users available to your table’s data source. Suppose it’s called userArray. Put user data for each user into that array. Suppose each user is stored as an object of type MYUser that responds to [user firstName] and [user lastName] calls with the first and the last name of the user.

    Now put this code into your table’s data source:

    MYUser *user = [userArray objectAtIndex:indexPath.row];
    NSString *cellValue = [NSString stringWithFormat:@"%@ %@", [user firstName], [user lastName]];
    cell.textLabel.text = cellValue;
    

    EDIT : If your menuArray stores the first and the last names in separate consecutive string objects, you can still do it (though I recommend against it, because it will be confusing to people who maintain your program in the future).

    First, you need to change the method that returns the number of items in the table to return menuArray.count / 2 instead of just menuArray.count. Then you can modify your code as follows:

    NSString firstName = [menuArray objectAtIndex:indexPath.row/2];
    NSString lastName = [menuArray objectAtIndex:indexPath.row/2 + 1];
    NSString *cellValue = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
    cell.textLabel.text = cellValue;
    

    EDIT2 : Here is how you create a class that stores the first and the last name of a user. You add the @interface part to MYUser.h, and the @implementation part to MYUser.m file. You then import MYUser.h in .m files from which you reference MYUser class, and use initWithFirstName:andLastName: to initialize new instances. The sample below assumes that you use ARC:

    MYUser.h file:

    #import <UIKit/UIKit.h>
    @interface MYUser : NSObject
    @property (nonatomic, strong) NSString *firstName;
    @property (nonatomic, strong) NSString *lastName;
    -(id)initWithFirstName:(NSString*)first andLastName:(NSString*)last;
    @end
    

    MYUser.m file:

    @implementation MYUser
    @synthesize firstName, lastName;
    -(id)initWithFirstName:(NSString*)first andLastName:(NSString*)last {
        self = [super init];
        if (self) {
            self.firstName = first;
            self.lastName = last;
        }
        return self;
    }
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've ran into one small problem. I am dynamically generating some controls and place
Just started working with android and ran into a small problem. I am have
I currently ran into a problem I can not really solve by myself: I
In my program, I ran into a problem writing a small number to a
I'm doing a project for a comapny and I ran into a small problem
I've just began learning Python and I've ran into a small problem. I need
Ran into a small problem when change to OpenLayers 2.11 with showing the length
I kinda ran into a problem, I'm working on a small image site for
I ran into the problem that my primary key sequence is not in sync
This is the problem I ran into long time ago. I thought I may

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.