jarus
jarus

Reputation: 1873

how is the function called in xcode

im new to xcode and i am doing this code to fill up the table view with annotation titles but the function gets called more than once and the table cells are filled with all repeated values , how is the function called in xcode , how can i stop this function from getting called more than once

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    NSLog(@"this is a test text            ");
    NSMutableArray *annotations = [[NSMutableArray alloc] init];
    int i=0;
    if(indexPath.section == 0)
    {
        for(iCodeBlogAnnotation *annotation in [map annotations])
        {
            i++;
            NSLog(@"this is the no %d",i);
            [annotations addObject:annotation]; 
        }

        cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];
    }

    return cell;
}

Any help would be deeply appreciated , Thank you for your help in advance

Upvotes: 0

Views: 683

Answers (3)

deanWombourne
deanWombourne

Reputation: 38475

You can't really control when it's called. It's called each time your tableview wants to display a new cell. You use the indexPath to determine what to put in that cell. It's called at least once per cell that's on the screen (sometimes more if the table is scrolled up and down).

You don't need to create the temporary array each time this function is called, just use [map annotations] directly :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // There will be one row per annotation
    return [[map annotations] count]
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Put the text from this annotation into this cell
    cell.textLabel.text = [[[map annotations] objectAtIndex:indexPath.row] title];

    return cell;
}

I hope I've understood your question. If not, please tell me in the comments below!

Upvotes: 2

Anomie
Anomie

Reputation: 94804

The function is called whenever the UITableView does not already have a UITableViewCell for a particular index path and needs one. Note that it may be called multiple times for an index path, as a result of user scrolling (to save memory, cells that are offscreen may be reused or released) or calls to reloadData and related functions or insertRowsAtIndexPaths:withRowAnimation: and related functions. You cannot (and really do not want to) prevent it from being called more than once.

That said, assuming that [map annotations] returns an ordered collection of some sort that is ordered the same way each time, your code should be doing what you want (even though it is very inefficient). More detail on the problem would be helpful.

Upvotes: 0

bbum
bbum

Reputation: 162712

It isn't a function, it is a method.

It is called by a table view as the table view draws cells. It will be called once per cell and, sometimes, more than once per cell depending on what the user is doing.

You don't push data into a table view, it asks you for cell contents.

Asking "how can i stop this function from getting called more than once?" indicates that you don't understand table views (it is confusing if you've come from the "push" model of UI programming). Start with the TableView programming guide.

Upvotes: 1

Related Questions