简体   繁体   English

使用NSMutableArray将数据添加到tableView

[英]Adding data to a tableView using a NSMutableArray

I'm having a problem adding an item to my tableView. 我在将项目添加到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. 我曾经在我的应用程序的开头初始化一个空的tableView,并在每次tableView再次出现且变量中有一个项目时,将其填充了扫描的项目。

Initialization of the tableView: tableView的初始化:

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:nil];
self.listArray = array;

TableView Data Source: 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;
}

(Not the whole thing but the ones I changed) (不是全部,而是我改变的)

As you may have seen I use an NSMutableArray to add items to my tableView. 如您所见,我使用NSMutableArray将项目添加到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. / edit:尝试指定我的问题:来自变量的数据写在TableViewCell中,但是如果我扫描另一个,则另一个将被替换。 Not sure if it's a problem with my array or my tableView... 不知道这是我的数组还是tableView问题...

/edit No.2: Found out(thanks to fzwo) that my array isn't working correctly. / edit No.2:发现(由于fzwo)我的阵列无法正常工作。 It just doesn't grow by an addObject: or insertObject:atIndex: command. 它只是不会因addObject:或insertObject:atIndex:命令而增长。 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 但我只是不明白为什么... :((我正在做的是: [listArray addObject:sharedGS.strEAN];在一行中没有那么多错误空间。也许我太愚蠢以至于无法识别我的内容做错了:D

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. 您声明您的问题是“将项目添加到我的tableView中”,因为您正在将对象添加到数组中,我猜问题是您没有重新加载表或缺少数据源绑定。

You have not actually asked any question (even if you added info to "specify your question") so a wild guess, after 您实际上没有问过任何问题(即使您添加了信息以“指定您的问题”),因此在

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

put

[yourTableView reloadData];

Are you intentionally adding new items to the top of the table ? 您是否有意在表格顶部添加新项目? otherwise you could do [listArray addObject:sharedGS.strEAN]; 否则,您可以执行[listArray addObject:sharedGS.strEAN]; to add new items to the bottom 在底部添加新项目

Otherwise it's worth noting that you are misusing dequeueReusableCellWithIdentifier, look at the example below for proper usage: 否则,值得注意的是您滥用了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];
}

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

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