在ios中覆盖UItable View Cell中的内容

在我的表格视图单元格中,我需要显示我的数组元素的内容在一个单元格中,如果我插入任何新的内容,以前的内容不会被覆盖。以前的内容和我的新内容应按顺序显示。这里是我的代码

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

我与我的代码面临的问题是每个string显示在每个单元格,如果我插入任何新的string以前的内容都不见了,只有新的内容显示。

为了清楚这个问题的想法,我试图实施“join购物车”服务的网上购物。根据每个概念的项目必须从各种产品添加,并保存产品信息,并必须显示在表视图中的细节但我不明白

请指导请..提前感谢..

  • (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; } 

    使用ReusablecellIdentifier无所以它正常工作…..

不知道你的cellForRowAtIndexPath是正确的。 看起来你没有检查是否cell是零。 如果你是(不知何故在括号内),那么你似乎只是更新lab.text中,如果(cell == nil)语句。

它可能应该是更类似于以下内容的东西:

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

您在cellForRowAtIndexPath中的代码是有点closures。 在初始化之前,您应该检查单元格是否为零。 看起来像你从某处复制括号,但省略了条件。

除此之外,你没有代码来改变被重用单元格的单元格的标签。 你只能设置新的文本。 一个容易跟踪的标签重用它是给它一个标签。

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

让你的“finarray”在每个对象中存储一个数组(我的意思是你需要一个二维数组)。 在cellForRowAtIndexPath您将通过indexPath.rowfinarray提取数组,并通过提取的数组循环来填充内容。 你的finarray中的每个数组第一次只包含1个对象。 通过改变单元格中的内容,你实际上会在编辑单元格索引处的数组中增加一个对象。 然后,通过重新加载数据,表格将显示新添加的数据。 确保您pipe理编辑的单元格的行的高度,因为它们需要增加。

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