DanielPrasath 0 Newbie Poster

In my table view cell i need to display the contents of my array elements in a single cell and if i insert any new contents the previous contents are not to be overwritten.The previous content and my new content should be displayed in order.Here is my code

#import "Carttable.h" 

@interface Carttable () 

@end 

@implementation Carttable 
@synthesize cnfqty,cnfrate,cnfimage,cnftitle,finarray; 


- (id)initWithStyle:(UITableViewStyle)style 
{ 
self = [super initWithStyle:style]; 
if (self) { 
// Custom initialization 

} 
return self; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

// Uncomment the following line to preserve selection between presentations. 
// self.clearsSelectionOnViewWillAppear = NO; 

// Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
// self.navigationItem.rightBarButtonItem = self.editButtonItem; 

NSMutableArray *arr1=[NSMutableArray arrayWithObjects:cnfqty, nil]; 
NSMutableArray *arr2=[NSMutableArray arrayWithObjects:cnftitle, nil]; 
NSMutableArray *arr3=[NSMutableArray arrayWithObjects:cnfrate, nil]; 

NSMutableArray *arrMain=[NSMutableArray arrayWithObjects:arr1, arr2, arr3, nil]; 


finarray=[[NSMutableArray alloc]init]; 
[finarray addObjectsFromArray:arrMain]; 
NSLog(@"%@",finarray); 


} 

- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

// Return the number of sections. 
return 1; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

return 150.0; 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
return [finarray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
{ 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 


} 
  cell.textlabel.text=[NSString stringWithFormat:@"%@",finarray];
return cell; 


}

The problem which i'm facing with my code is each string is displayed in each cell and if i insert any new string previous contents are gone and only new contents are displayed.

To get clear idea about this question i'm trying to implement "add to cart" service for online shopping.As per concept the items have to be added from various products and it saves the product info and have to display the details in Table view.But i'm not getting it..

Kindly Guide please..Thanks in advance..

My output is like this first image

But i need to get display as second image which done in Android..

Dani AI

Generated

— the symptoms you describe (each cell showing the same data, new additions replacing previous ones) point to two common problems: the table’s data source is being rebuilt or replaced instead of appended to, and the cell is being configured with the entire data array rather than the single item for that row.

A practical, reliable approach:

  • Keep one mutable array as the table’s data source (for example cartItems). Do not re-create that array every time the view appears. Initialize it once and append each new product as a single model object (NSDictionary or a small CartItem class) that contains title, qty, price, image, etc.
  • When a product is added, push one model object onto the array and call reloadData on the table view (or insertRowsAtIndexPaths: with animation).
  • In tableView:cellForRowAtIndexPath: pull the model for indexPath.row and populate the cell labels from that single model. Do not set the cell text to the whole array.

Example (Objective-C, different names than your snippet):

if (!self.cartItems) { self.cartItems = [NSMutableArray array]; }
NSDictionary *product = @{@"title":titleString, @"qty":qtyString, @"price":priceString};
[self.cartItems addObject:product];
[self.tableView reloadData];
NSDictionary *product = self.cartItems[indexPath.row];
cell.textLabel.text = product[@"title"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ x %@", product[@"qty"], product[@"price"]];

Troubleshooting checklist:

  • Verify numberOfRowsInSection: returns cartItems.count.
  • Make sure the array is a strong/retained property so it isn’t deallocated or reset.
  • If items are added from another view controller, use a delegate, notification, or a shared CartManager singleton so the same array is modified.
  • For persistence between launches, serialize the array (NSUserDefaults, file, or Core Data).

Apple’s Table View guide is a useful refresher: .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.