如何动态地在运行时在nsmutable数组中添加元素?

时间:2023-01-27 13:37:01

i have an nsmutable array , so i want to show elements of array on tableview but when i

我有一个nsmutable数组,所以我想在tableview上显示数组的元素但是当我

scroll tableview cells new elements should be added .

滚动tableview单元格应添加新元素。

there is a confusion how can i add elements on run time when table view scrolled . ??

如果在滚动表视图时如何在运行时添加元素,则会产生混淆。 ??

- (void)viewDidLoad
{   
    tableList1 = [[NSMutableArray alloc]initWithObjects:@"John",@"Dick",@"nihyan",@"alex",@"niv",nil];
    NSLog(@" arr count in Friends list   %i",tableList2.count);
    [super viewDidLoad];
}

1 个解决方案

#1


3  

You are indeed confused :)

你真的很困惑:)

Lets separate the answer to 2 scenarios:

让我们将答案分为两个场景:

  1. You know all your table view data from the beginning: You don't need to add them yourself, you just need to pass the table view the full array with all the data that you need in CellForRowAtIndexPath and the table view will reuse the cells and display the next line when the user scrolls.
  2. 您从一开始就知道所有的表视图数据:您不需要自己添加它们,只需要将表视图传递给包含CellForRowAtIndexPath中所需的所有数据的完整数组,表视图将重用单元格和用户滚动时显示下一行。

  3. You need to add Items: The simplest way will be to add the new data to the array:

    您需要添加项目:最简单的方法是将新数据添加到数组:

    [tableList1 addObject:YourObject];
    

    And then call:

    然后打电话:

     [self.yourTableView reloadData];
    

You should know that there are ways to add a single row with out refreshing all the table.

您应该知道有一些方法可以添加一行而不刷新所有表。

You can learn more here : UITableView Guide

您可以在此处了解更多信息:UITableView指南

#1


3  

You are indeed confused :)

你真的很困惑:)

Lets separate the answer to 2 scenarios:

让我们将答案分为两个场景:

  1. You know all your table view data from the beginning: You don't need to add them yourself, you just need to pass the table view the full array with all the data that you need in CellForRowAtIndexPath and the table view will reuse the cells and display the next line when the user scrolls.
  2. 您从一开始就知道所有的表视图数据:您不需要自己添加它们,只需要将表视图传递给包含CellForRowAtIndexPath中所需的所有数据的完整数组,表视图将重用单元格和用户滚动时显示下一行。

  3. You need to add Items: The simplest way will be to add the new data to the array:

    您需要添加项目:最简单的方法是将新数据添加到数组:

    [tableList1 addObject:YourObject];
    

    And then call:

    然后打电话:

     [self.yourTableView reloadData];
    

You should know that there are ways to add a single row with out refreshing all the table.

您应该知道有一些方法可以添加一行而不刷新所有表。

You can learn more here : UITableView Guide

您可以在此处了解更多信息:UITableView指南