overwritting of contents in UItable View Cell in i

2019-09-09 09:48发布

问题:

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

- (void)viewDidLoad
{
[super viewDidLoad];


NSArray *arr=[NSArray arrayWithObjects:cnfqty,cnftitle,cnfrate, nil];

//cnfqty,cnftitle,cnfrate are NSString

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


}

- (void)viewDidUnload
{
[super viewDidUnload];

}

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

#pragma mark - Table view data source

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

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

- (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];

    UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
    lab.text=[finarray objectAtIndex:indexPath.row];
    [cell.contentView addSubview:lab];

}
// Configure the cell...

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..

回答1:

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

       UITableViewCell *cell = (UITableViewCell*)[self.YourTableName dequeueReusableCellWithIdentifier:nil];        
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    }
    
    
        return cell; 
    }
    

    use ReusablecellIdentifier nil so it working correctly.....



回答2:

Not sure your cellForRowAtIndexPath is correct. Looks like you are not checking to see if cell is nil. If you are (somehow inside the brackets), then you seem to only be updating lab.text within that if(cell == nil) statement.

It should probably be something more like the following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"Cell";
    UILabel *lab; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  

    if(cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];      
        lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
        lab.tag = 1; // Really any arbitraty number
        [cell.contentView addSubview:lab];  
    }else{
        lab = [cell.contentView viewWithTag:1]
    }

    // Configure the cell... 
    lab.text=[finarray objectAtIndex:indexPath.row];      

    return cell; 
}


回答3:

Your code in cellForRowAtIndexPath is a bit off. You should be checking if the cell is nil before initializing it. Seems like you copied the brackets from somewhere, but left out the conditional.

Aside from that, you have no code to change the label of the cell for a cell being reused. You only set the text on new ones. One easy to to keep track of the label to reuse it is to give it a tag.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if(cell == nil)
  { // set up brand new cell
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
      lab.tag = 150; // some int you can keep track of, possibly make it a define.
      [cell.contentView addSubview:lab];
  }
  // configure cell
  UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:150]; // there's that tag again
  cellLabel.text = [finarray objectAtIndex:indexPath.row];
  return cell;
}


回答4:

Make your "finarray" to store an array in each object (I mean you need a kind of bi-dimensional array). In cellForRowAtIndexPath you will extract the array from finarray by the indexPath.row, and make a loop through the extracted array to populate your content. The first time each array in your finarray will contain only 1 object. By changing the content in the cell, you actually will add one more object into the array located at index of your edited cell. Then, by reloading data, the table will display the new added data. Make sure you manage the height of the rows for the edited cells,as they will need to be increased.

- (void)viewDidLoad
{
[super viewDidLoad];

//bla bla bla whatever you had here

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:arr];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//bla bla bla the code to initiate the cell

NSArray *tmpArray = [finarray objectAtIndex:indexPath.row];

for(int i = 0; i<tmpArray.count;i++){
  //create the label
  //set the frame, making sure that origin.y is multiplied by "i"
  //set label's text as [tmpArray objectAtIndex:i]
  //add the label to cell
}

return cell;
}