使用 NSMutableArray 将数据添加到 tableView
我在向 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 的问题...
/edit No.2:发现(感谢 fzwo)我的数组无法正常工作。它只是不会通过 addObject: 或 insertObject:atIndex: 命令增长。但我就是不明白为什么......:( 我所做的一切: [listArray addObject:sharedGS.strEAN];
一行简单的错误没有那么多空间。也许我太愚蠢了,无法意识到自己做错了什么:D
I'm having a problem adding an item to my tableView.
I used to initialize an empty tableView at the start of my App and get it filled with scanned items every time the tableView reappears and there is an item in my variable.
Initialization of the tableView:
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:nil];
self.listArray = array;
TableView 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 [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;
}
(Not the whole thing but the ones I changed)
As you may have seen I use an NSMutableArray to add items to my tableView.
So if an item ist scanned I'm adding it to my array like this:
[listArray insertObject:sharedGS.strEAN atIndex:0]; //using a shared Instance where I implemented my variable.
I also tried to use an variable to extend my Index every time a new Item is added, but it won't work both ways.
I'm quite new to programming so an not-too-hard-to-understand-answer would be quite nice ;)
If there's any information missing, feel free to ask.
/edit: Trying to specify my question: The data from the variable is written in a TableViewCell, but if I scan another one the other one is just being replaced. Not sure if it's a problem with my array or my tableView...
/edit No.2: Found out(thanks to fzwo) that my array isn't working correctly. It just doesn't grow by an addObject: or insertObject:atIndex: command. But I just don't get why... :(
All I'm doing: [listArray addObject:sharedGS.strEAN];
not that much space for errors in one simple line. Maybe I'm just too stupid to recognize what I'm doing wrong:D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您声明您的问题是“将项目添加到我的 tableView”,因为您正在将对象添加到数组中,所以我猜测问题是您没有重新加载表或者它缺少 dataSource 绑定。
您实际上并没有提出任何问题(即使您添加了信息来“指定您的问题”),因此您可以大胆猜测,在
放置
之后您是否故意将新项目添加到表格顶部?否则你可以做
[listArray addObject:sharedGS.strEAN];
将新项目添加到底部否则值得注意的是,您误用了 dequeueReusableCellWithIdentifier,请查看下面的示例以了解正确用法:
You state that your problem is "adding an item to my tableView" , since you are adding the object to your array i am guessing the problem is that you are not reloading the table or that it is missing the dataSource binding.
You have not actually asked any question (even if you added info to "specify your question") so a wild guess, after
put
Are you intentionally adding new items to the top of the table ? otherwise you could do
[listArray addObject:sharedGS.strEAN];
to add new items to the bottomOtherwise it's worth noting that you are misusing dequeueReusableCellWithIdentifier, look at the example below for proper usage: