简体   繁体   English

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

[英]how to add elements in nsmutable array at run time dynamically?

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

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. 您从一开始就知道所有表视图数据 :无需自己添加它们,只需要将表视图与所需的所有数据一起传递给完整数组,即可在CellForRowAtIndexPath ,表视图将重用单元格并当用户滚动时显示下一行。
  2. You need to add Items : The simplest way will be to add the new data to the array: 您需要添加Items :最简单的方法是将新数据添加到数组中:

     [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指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM