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 7049809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:04:00+00:00 2026-05-28T03:04:00+00:00

Xcode 4.2 iPhone Project I am trying to create a tableView that’s populated from

  • 0

Xcode 4.2
iPhone Project

I am trying to create a tableView that’s populated from an NSMutableArray. Currently when I run the simulator the tableView in question shows 5 rows (this is correct) but all rows have the same string (this obviously is not correct). The string being shown is from the LAST object defined in my NSMutableArray. Here’s the code:

- (void)viewDidLoad
{

[super viewDidLoad];

codes = [[NSMutableArray alloc] init];
TableList *listItem = [[TableList alloc] init];

[listItem setCodeTitle:@"Test Name 1"];
[listItem setCodeNumber:@"101"];
[listItem setCodeDescription:@"This is the description for 101."];
[codes addObject:listItem];

[listItem setCodeTitle:@"Test Name 2"];
[listItem setCodeNumber:@"102"];
[listItem setCodeDescription:@"This is the description for 102."];
[codes addObject:listItem];

[listItem setCodeTitle:@"Test Name 3"];
[listItem setCodeNumber:@"103"];
[listItem setCodeDescription:@"This is the description for 103."];
[codes addObject:listItem];

[listItem setCodeTitle:@"Test Name 4"];
[listItem setCodeNumber:@"104"];
[listItem setCodeDescription:@"This is the description for 104."];
[codes addObject:listItem];

[listItem setCodeTitle:@"Test Name 5"];
[listItem setCodeNumber:@"105"];
[listItem setCodeDescription:@"This is the description for 105."];
[codes addObject:listItem];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return [codes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CodeCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell...
TableList *current = [codes objectAtIndex:indexPath.row];
cell.textLabel.text = [current codeTitle];

return cell;
}

I hope this is painfully obvious to anyone with more experience than me… I have very little.

  • 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-28T03:04:00+00:00Added an answer on May 28, 2026 at 3:04 am

    You’re storing the exact same object 5 times into your array. You seem to be under the misapprehension that -addObject: will copy the object it’s handed, but it won’t. The simple fix is to change your -addObject: lines to say

    [codes addObject:[[listItem copy] autorelease]];
    

    This is, of course, assuming your TableList class conforms to <NSCopying>. If it doesn’t, you’ll probably want to do so. If you’re unwilling to support copying in that class, then you’ll have to create a new TableList object each time.

    Also, unless you’re using ARC, you’re leaking your TableList object.


    In order to support -copy, you must conform to <NSCopying>, which is a protocol that declares the method -copyWithZone: (though the zone argument is a historical curiosity that should be ignored). To do this, modify your @interface line to look like

    @interface TableList : NSObject <NSCopying>
    

    and then implement the method -copyWithZone:

    - (id)copyWithZone:(NSZone *)zone {
        // If our superclass conformed to <NSCopying> we'd call [super copy]
        // But it doesn't, so alloc/init a new instance
        TableList *newObj = [[TableList alloc] init];
        newObj.codeTitle = self.codeTitle;
        newObj.codeNumber = self.codeNumber;
        newObj.codeDescription = self.codeDescription;
        return newObj;
    }
    

    This is just a sample implementation. It assumes that those 3 properties are the only values on the object, and that simply assigning to the property is appropriate. If you have internal ivars that need to be copied over, you can get ivar access by using something like

    newObj->ivar = copyIvar(self->ivar); // self->ivar is the same as just plain ivar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build an xcode project and run it through the iPhone Simulator
Just replaced my XCode 4.0.1 with 4.2. Trying to run the iPhone game project
I am trying to build a project onto my iphone from Xcode 4, which
When I build my iPhone project, I get warnings from xcode about a few
Is it possible to open an iPhone application or an Xcode project from the
I'm trying to read data from 1.txt in my iphone project's resources folder using
I am trying to add unit tests to my iPhone project in Xcode. Everything
I'm trying to compile an iPhone app that uses CorePlot. I'm using XCode 4.
Something really weird is going on with Xcode and an iPhone project I'm working
I have made a ViewController in XCode for an iPhone project I'm working on,

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.