Objective c 使用NSMutableArray向tableView添加数据

Objective c 使用NSMutableArray向tableView添加数据,objective-c,uitableview,nsmutablearray,Objective C,Uitableview,Nsmutablearray,向tableView添加项目时遇到问题 我曾经在应用程序开始时初始化一个空的tableView,每次tableView重新出现并且我的变量中有一个项目时,我都会用扫描的项目填充它 tableView的初始化: NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:nil]; self.listArray = array; TableView数据源: - (NSInteger)numberOfSectionsInTable

向tableView添加项目时遇到问题

我曾经在应用程序开始时初始化一个空的tableView,每次tableView重新出现并且我的变量中有一个项目时,我都会用扫描的项目填充它

tableView的初始化:

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:nil];
self.listArray = array;
TableView数据源:

- (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 [self.listArray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    if(section == 0)
        return @"Eingescannte Artikel:";
    else
        return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"testCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    // Configure the cell...
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listArray objectAtIndex:row];//[NSString stringWithFormat:@"Das ist Zeile %i", indexPath.row];
    return cell;
}
(不是全部,而是我改变的那些)

正如您可能已经看到的,我使用NSMutableArray将项添加到我的tableView中

因此,如果扫描了一个项目,我将其添加到数组中,如下所示:

[listArray insertObject:sharedGS.strEAN atIndex:0]; //using a shared Instance where I implemented my variable.
我还尝试在每次添加新项时使用一个变量来扩展我的索引,但这两种方法都不行

我对编程很陌生,所以一个不太难理解的答案会很好;)

如果缺少任何信息,请随时询问

/编辑:尝试指定我的问题:变量中的数据写入TableViewCell,但如果我扫描另一个,另一个刚好被替换。不确定是我的数组还是我的tableView有问题

/编辑2:发现(感谢fzwo)我的阵列工作不正常。它不会通过addObject:或insertObject:atIndex:命令增长。但我不明白为什么…:(
我所做的一切:
[listArray addObject:sharedGS.strEAN];
在一行中没有太多的错误空间。也许我只是太愚蠢了,没有意识到我做错了什么:D

你说你的问题是“将一个项目添加到我的tableView”,因为您要将对象添加到数组中,我猜问题在于您没有重新加载表,或者它缺少数据源绑定

你实际上没有问过任何问题(即使你在“指定你的问题”中添加了信息),所以在

[listArray insertObject:sharedGS.strEAN atIndex:0];

您是否有意将新项目添加到表的顶部?否则您可以这样做
[listary addObject:sharedGS.strEAN];
在底部添加新项目

否则值得注意的是,您误用了dequeueReusableCellWithIdentifier,请查看以下示例以了解正确用法:

// Try to retrieve from the table view a now-unused cell with the given identifier
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

// If no cell is available, create a new one using the given identifier
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}

您声明您的问题是“向我的tableView添加一个项”,因为您正在将对象添加到数组中,所以我猜问题在于您没有重新加载表或者它缺少数据源绑定

你实际上没有问过任何问题(即使你在“指定你的问题”中添加了信息),所以在

[listArray insertObject:sharedGS.strEAN atIndex:0];

您是否有意将新项目添加到表的顶部?否则您可以这样做
[listary addObject:sharedGS.strEAN];
在底部添加新项目

否则值得注意的是,您误用了dequeueReusableCellWithIdentifier,请查看以下示例以了解正确用法:

// Try to retrieve from the table view a now-unused cell with the given identifier
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

// If no cell is available, create a new one using the given identifier
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}

不知道它是否改变了什么,但它们仍在相互替换:(@dequeueReusableCellWithIdentifier:我正在使用新的故事板创建我的tableView并添加一个原型单元格。不确定,但我想我在某个地方读到此方法是自动实现的。无论如何,我会尝试!我找到了你的“UITableViewCell*单元格”=[tableView dequeueReusableCellWithIdentifier:MyIdentifier]“在我的代码中,但添加createNewCell时,他告诉我它已被弃用。未弃用的版本是
initWithStyle:reuseIdentifier:
,正如文档所告诉您的那样。不过没关系,旧版本应该仍然有效。现在尝试了两个版本,也尝试了addObject版本,但无论如何,只有在cell上替换旧版本。不要“我不知道它是否改变了什么,但它们仍在相互替换:(@dequeueReusableCellWithIdentifier:我正在使用新的故事板创建我的tableView并添加一个原型单元格。不确定,但我想我在某个地方读到此方法是自动实现的。无论如何,我会尝试!我找到了您的“UITableViewCell*单元格”=[tableView dequeueReusableCellWithIdentifier:MyIdentifier]“在我的代码中,但添加createNewCell时,他告诉我它已被弃用。未弃用的版本是
initWithStyle:reuseIdentifier:
,文档会告诉你。不过没关系,旧版本应该仍然可以工作。现在尝试了两个版本,也尝试了addObject版本,但无论如何,只有在cell上替换旧版本。是吗你确定你的数组正在增长吗?将
NSLog(@“Anzahl der EAN code:%d\n主代码:%@),listary.count,[listary objectAtIndex:0]);
放在调用
[tableView reloadData]的任何地方
.Ah!看起来它根本没有增长。但是使用addObject或insertObject:atIndex:方法应该会迫使数组增长,或者我错了吗?你是对的,它应该增长。也许你不小心在其他地方删除了对象?在我的项目中搜索了我的“listArray”,但除了在我的-(void)“tableViewEditingMethod”中,没有发现任何删除操作(但在那里没问题)。但我在viewDidLoad方法中用testData填充数组。据我所知,它只加载了一次,之后ViewWillAppeal方法将取代它,但这将是每次加载视图时重置数组的唯一位置(有点自我失败)。它需要处于-(void)状态吗init?
init
在对象的生命周期中只调用一次:在开始时。
viewDidLoad
通常在ViewController的生命周期中只调用一次。
ViewWillAspect
在ViewController的视图即将出现在屏幕上时调用。根据您的体系结构,可以调用一次或多次,或者根本没有。如果让我猜一猜,我会说在代码中可能只调用了一次,但如果没有更多信息,就不可能说。请确保在出现新数据时更新数组和TableView。使用NSLog或断点了解调用更新代码的频率。是否确定数组正在增长?输入
NSLog(@“Anzahl der EAN代码:%d\n主代码:%@”,listary.count,[listary objectAtIndex:0]);无论您在哪里调用[tableView reloadData]。啊!