1

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

14
  • Are you sure your array is growing? Put NSLog(@"Anzahl der EAN-Codes: %d\nErster Code: %@", listArray.count, [listArray objectAtIndex:0]); wherever you're calling [tableView reloadData]. Commented Feb 15, 2012 at 11:34
  • Ah! Looks like it isn't growing at all. But using the addObject or insertObject:atIndex: method should force the array to grow, or am I wrong? Commented Feb 17, 2012 at 13:55
  • You're right, it should grow. Maybe you're accidentally removing objects somewhere else? Commented Feb 17, 2012 at 14:21
  • Searched my project for my "listArray" but found no removing except in my-(void)"tableViewEditingMethod"(but afaik it's ok there). BUT I'm filling my array with testData in my viewDidLoad-method. As I understood it's just loaded once and afterwards the viewWillAppear method will take its place but this would be the only place to reset my array every time the view gets loaded (sort of self fail). Does it need to be in an -(void) init? Commented Feb 17, 2012 at 14:36
  • init is called exactly once in the lifetime of an object: At the beginning. viewDidLoad is usually only called once in the lifetime of a ViewController. viewWillAppear is called when the ViewController's view is about to appear on screen. Depending on your architecture, it can be called once, many times, or not at all. If I had to take a guess, I'd say it is probably only called once in your code, but it's impossible to say without more info. Make sure you update your Array and TableView whenever there's new data. Use NSLog or breakpoints to find out how often your update code is called. Commented Feb 17, 2012 at 14:53

1 Answer 1

1

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

[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]; 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:

// 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];
}
Sign up to request clarification or add additional context in comments.

4 Comments

don't know if it changed anything but they're still replacing each other :( @dequeueReusableCellWithIdentifier: I'm using the new storyboard to create my tableView and added a prototype cell. Not sure, but I think I read somewhere this method is implemented automatically. Anyway I'll try it!
Well I found your "UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]" in my code but adding the createNewCell-Thing he's telling me it's deprecated.
The non-deprecated version is initWithStyle:reuseIdentifier:, as the documentation will tell you. Doesn't matter though, the old one should still work.
Tried both versions now and the addObject one too, but anyway there just on cell replacing the old one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.